chore(report): 清理临时资源诊断代码
This commit is contained in:
+1
-229
@@ -13,7 +13,6 @@ using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Lskj.Control;
|
||||
using Lskj.Model;
|
||||
@@ -24,7 +23,6 @@ using Lskj.Business.Impl;
|
||||
using Lskj.Business;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Lskj.Report
|
||||
@@ -42,8 +40,6 @@ namespace Lskj.Report
|
||||
mReportGridLocalizerViews =
|
||||
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
||||
private bool mReportGridResourcesReleased;
|
||||
private int mDisposedReportGridViewCount;
|
||||
private int mRemovedGridLocalizerHandlerCount;
|
||||
|
||||
public FrmMain()
|
||||
{
|
||||
@@ -99,19 +95,12 @@ namespace Lskj.Report
|
||||
|
||||
private void OnClose(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
WriteReportLifecycleDiagnostics("closing-before-close-module");
|
||||
this.mControl.CloseModule();
|
||||
WriteReportLifecycleDiagnostics("closing-after-close-module");
|
||||
}
|
||||
|
||||
private void OnFrmMainDisposed(object sender, EventArgs e)
|
||||
{
|
||||
DisposeReportGridViews();
|
||||
WriteReportLifecycleDiagnostics(
|
||||
"disposed-event;disposedGridViews=" +
|
||||
mDisposedReportGridViewCount +
|
||||
";gridLocalizerRemoved=" +
|
||||
mRemovedGridLocalizerHandlerCount);
|
||||
mReportGridLocalizerViews.Clear();
|
||||
mPreexistingGridLocalizerViews.Clear();
|
||||
this.FormClosing -= OnClose;
|
||||
@@ -142,7 +131,6 @@ namespace Lskj.Report
|
||||
}
|
||||
|
||||
mReportGridResourcesReleased = true;
|
||||
WriteReportLifecycleDiagnostics("dispose-before-gridviews");
|
||||
CaptureReportGridLocalizerViews();
|
||||
if (this.mControl != null)
|
||||
{
|
||||
@@ -151,8 +139,6 @@ namespace Lskj.Report
|
||||
|
||||
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> disposedViews =
|
||||
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
||||
int disposeErrorCount = 0;
|
||||
int detachedDataSourceCount = 0;
|
||||
foreach (System.Windows.Forms.Control control in
|
||||
EnumerateControls(this.mControl))
|
||||
{
|
||||
@@ -165,12 +151,10 @@ namespace Lskj.Report
|
||||
if (gridControl.DataSource != null)
|
||||
{
|
||||
gridControl.DataSource = null;
|
||||
detachedDataSourceCount++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
disposeErrorCount++;
|
||||
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
||||
}
|
||||
|
||||
@@ -191,7 +175,6 @@ namespace Lskj.Report
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
disposeErrorCount++;
|
||||
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
||||
}
|
||||
}
|
||||
@@ -216,33 +199,16 @@ namespace Lskj.Report
|
||||
dataSourceProperty.GetValue(control, null) != null)
|
||||
{
|
||||
dataSourceProperty.SetValue(control, null, null);
|
||||
detachedDataSourceCount++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
disposeErrorCount++;
|
||||
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int trackedGridViews = mReportGridLocalizerViews.Count;
|
||||
int removedGridLocalizerHandlers =
|
||||
ReleaseTrackedGridLocalizerViews(disposedViews);
|
||||
mDisposedReportGridViewCount = disposedViews.Count;
|
||||
mRemovedGridLocalizerHandlerCount = removedGridLocalizerHandlers;
|
||||
WriteReportLifecycleDiagnostics(
|
||||
"dispose-after-gridviews;dispose-attempts=" +
|
||||
disposedViews.Count +
|
||||
";dispose-errors=" +
|
||||
disposeErrorCount +
|
||||
";detachedDataSources=" +
|
||||
detachedDataSourceCount +
|
||||
";trackedGridViews=" +
|
||||
trackedGridViews +
|
||||
";gridLocalizerRemoved=" +
|
||||
removedGridLocalizerHandlers);
|
||||
ReleaseTrackedGridLocalizerViews(disposedViews);
|
||||
}
|
||||
|
||||
private void CaptureReportGridLocalizerViews()
|
||||
@@ -454,200 +420,6 @@ namespace Lskj.Report
|
||||
handler.Target is DevExpress.XtraGrid.Views.Base.BaseView;
|
||||
}
|
||||
|
||||
private void WriteReportLifecycleDiagnostics(string phase)
|
||||
{
|
||||
if (!IsReportDiagnosticsEnabled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
LogHelper.Instance.WriteLog(
|
||||
"REPORT_DIAGNOSTIC phase=" +
|
||||
(phase ?? string.Empty) +
|
||||
" " +
|
||||
GetReportGridDiagnostics(),
|
||||
"ResourceDiagnostics.Report");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Diagnostics must never interfere with module close/dispose.
|
||||
}
|
||||
}
|
||||
|
||||
private string GetReportGridDiagnostics()
|
||||
{
|
||||
int controlCount = 0;
|
||||
int gridControlCount = 0;
|
||||
int viewCount = 0;
|
||||
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> views =
|
||||
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
||||
StringBuilder viewDetails = new StringBuilder();
|
||||
|
||||
try
|
||||
{
|
||||
if (this.mControl != null)
|
||||
{
|
||||
foreach (System.Windows.Forms.Control control in EnumerateControls(this.mControl))
|
||||
{
|
||||
controlCount++;
|
||||
DevExpress.XtraGrid.GridControl gridControl =
|
||||
control as DevExpress.XtraGrid.GridControl;
|
||||
if (gridControl == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
gridControlCount++;
|
||||
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in
|
||||
gridControl.ViewCollection.Cast<DevExpress.XtraGrid.Views.Base.BaseView>())
|
||||
{
|
||||
if (view == null || !views.Add(view))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
viewCount++;
|
||||
if (viewDetails.Length < 1800)
|
||||
{
|
||||
if (viewDetails.Length > 0)
|
||||
{
|
||||
viewDetails.Append(",");
|
||||
}
|
||||
|
||||
viewDetails.Append(SafeViewDescription(view));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
viewDetails.Append(",enumeration-error=");
|
||||
viewDetails.Append(exception.GetType().Name);
|
||||
}
|
||||
|
||||
return "formDisposed=" +
|
||||
this.IsDisposed +
|
||||
";controlCount=" +
|
||||
controlCount +
|
||||
";gridControlCount=" +
|
||||
gridControlCount +
|
||||
";viewCount=" +
|
||||
viewCount +
|
||||
";views=" +
|
||||
viewDetails +
|
||||
";gridLocalizer=" +
|
||||
DescribeGridLocalizer();
|
||||
}
|
||||
|
||||
private static string SafeViewDescription(
|
||||
DevExpress.XtraGrid.Views.Base.BaseView view)
|
||||
{
|
||||
try
|
||||
{
|
||||
return view.GetType().FullName +
|
||||
":" +
|
||||
(view.Name ?? string.Empty).Replace(";", ",");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "<view-description-error>";
|
||||
}
|
||||
}
|
||||
|
||||
private static string DescribeGridLocalizer()
|
||||
{
|
||||
try
|
||||
{
|
||||
var active =
|
||||
DevExpress.XtraGrid.Localization.GridLocalizer.Active;
|
||||
if (active == null)
|
||||
{
|
||||
return "active=null";
|
||||
}
|
||||
|
||||
StringBuilder handlers = new StringBuilder();
|
||||
int handlerCount = 0;
|
||||
Type currentType = active.GetType();
|
||||
while (currentType != null && currentType != typeof(object))
|
||||
{
|
||||
FieldInfo[] fields = currentType.GetFields(
|
||||
BindingFlags.Instance |
|
||||
BindingFlags.Static |
|
||||
BindingFlags.Public |
|
||||
BindingFlags.NonPublic |
|
||||
BindingFlags.DeclaredOnly);
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
if (!typeof(Delegate).IsAssignableFrom(field.FieldType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object owner = field.IsStatic ? null : (object)active;
|
||||
Delegate value = field.GetValue(owner) as Delegate;
|
||||
if (value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Delegate handler in value.GetInvocationList())
|
||||
{
|
||||
handlerCount++;
|
||||
if (handlers.Length < 1800)
|
||||
{
|
||||
if (handlers.Length > 0)
|
||||
{
|
||||
handlers.Append(",");
|
||||
}
|
||||
|
||||
string targetType = handler.Target == null
|
||||
? "static"
|
||||
: handler.Target.GetType().FullName;
|
||||
handlers.Append(field.Name.Replace(";", ","));
|
||||
handlers.Append("->");
|
||||
handlers.Append(targetType.Replace(";", ","));
|
||||
handlers.Append(".");
|
||||
handlers.Append(handler.Method.Name.Replace(";", ","));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentType = currentType.BaseType;
|
||||
}
|
||||
|
||||
return "activeType=" +
|
||||
active.GetType().FullName.Replace(";", ",") +
|
||||
";handlerCount=" +
|
||||
handlerCount +
|
||||
";handlers=" +
|
||||
handlers;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return "localizer-error=" + exception.GetType().Name;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsReportDiagnosticsEnabled()
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.Exists(
|
||||
Path.Combine(
|
||||
AppDomain.CurrentDomain.BaseDirectory,
|
||||
"Logs",
|
||||
"ResourceDiagnostics",
|
||||
"resource-diagnostics.enabled"));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<System.Windows.Forms.Control> EnumerateControls(
|
||||
System.Windows.Forms.Control root)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user