182 lines
5.7 KiB
C#
182 lines
5.7 KiB
C#
using BaseFunction;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace NSAnalysis
|
|
{
|
|
/// <summary>
|
|
/// 刷新监控工具类,用于监控和诊断窗体刷新问题
|
|
/// </summary>
|
|
public static class RefreshMonitor
|
|
{
|
|
private static Dictionary<string, WindowStats> windowStats = new Dictionary<string, WindowStats>();
|
|
|
|
private class WindowStats
|
|
{
|
|
public DateTime LastRefreshTime { get; set; } = DateTime.Now;
|
|
public string LastVIN { get; set; } = "";
|
|
public int RefreshCount { get; set; } = 0;
|
|
public int ErrorCount { get; set; } = 0;
|
|
public int DataRetrievalCount { get; set; } = 0;
|
|
public int EmptyDataCount { get; set; } = 0;
|
|
public DateTime StartTime { get; set; } = DateTime.Now;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录刷新活动
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称</param>
|
|
/// <param name="vin">当前VIN码</param>
|
|
/// <param name="isSuccess">是否成功</param>
|
|
public static void LogRefreshActivity(string windowName, string vin, bool isSuccess = true)
|
|
{
|
|
if (!windowStats.ContainsKey(windowName))
|
|
{
|
|
windowStats[windowName] = new WindowStats();
|
|
}
|
|
|
|
var stats = windowStats[windowName];
|
|
stats.RefreshCount++;
|
|
stats.LastRefreshTime = DateTime.Now;
|
|
|
|
if (!isSuccess)
|
|
{
|
|
stats.ErrorCount++;
|
|
}
|
|
|
|
if (stats.LastVIN != vin)
|
|
{
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - VIN变更: {stats.LastVIN} -> {vin}, 刷新次数: {stats.RefreshCount}, 错误次数: {stats.ErrorCount}");
|
|
stats.LastVIN = vin;
|
|
}
|
|
|
|
// 每100次刷新记录一次统计信息
|
|
if (stats.RefreshCount % 100 == 0)
|
|
{
|
|
var successRate = ((stats.RefreshCount - stats.ErrorCount) * 100.0 / stats.RefreshCount);
|
|
var runTime = DateTime.Now - stats.StartTime;
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - 统计: 总刷新{stats.RefreshCount}次, 错误{stats.ErrorCount}次, 成功率: {successRate:F1}%, 运行时间: {runTime.TotalHours:F1}小时");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录数据库查询结果
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称</param>
|
|
/// <param name="queryType">查询类型</param>
|
|
/// <param name="result">查询结果</param>
|
|
/// <param name="rowCount">数据行数</param>
|
|
public static void LogDatabaseQuery(string windowName, string queryType, string result, int rowCount = 0)
|
|
{
|
|
if (!windowStats.ContainsKey(windowName))
|
|
{
|
|
windowStats[windowName] = new WindowStats();
|
|
}
|
|
|
|
var stats = windowStats[windowName];
|
|
stats.DataRetrievalCount++;
|
|
|
|
if (rowCount == 0)
|
|
{
|
|
stats.EmptyDataCount++;
|
|
}
|
|
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - 数据库查询: {queryType}, 结果: {result}, 行数: {rowCount}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录UI更新情况
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称</param>
|
|
/// <param name="updateType">更新类型</param>
|
|
/// <param name="controlCount">更新的控件数量</param>
|
|
public static void LogUIUpdate(string windowName, string updateType, int controlCount)
|
|
{
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - UI更新: {updateType}, 控件数量: {controlCount}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否长时间未刷新
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称</param>
|
|
/// <param name="maxMinutes">最大允许的未刷新分钟数</param>
|
|
/// <returns>是否超时</returns>
|
|
public static bool CheckRefreshTimeout(string windowName, int maxMinutes = 5)
|
|
{
|
|
if (!windowStats.ContainsKey(windowName))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var stats = windowStats[windowName];
|
|
var timeSinceLastRefresh = DateTime.Now - stats.LastRefreshTime;
|
|
if (timeSinceLastRefresh.TotalMinutes > maxMinutes)
|
|
{
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] 警告: {windowName} 已经 {timeSinceLastRefresh.TotalMinutes:F1} 分钟未刷新!");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置指定窗体的监控统计
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称,为空则重置所有</param>
|
|
public static void Reset(string windowName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(windowName))
|
|
{
|
|
windowStats.Clear();
|
|
MyBase.TraceWriteLine("[RefreshMonitor] 所有窗体监控统计已重置");
|
|
}
|
|
else if (windowStats.ContainsKey(windowName))
|
|
{
|
|
windowStats[windowName] = new WindowStats();
|
|
MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} 监控统计已重置");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定窗体的统计信息
|
|
/// </summary>
|
|
/// <param name="windowName">窗体名称</param>
|
|
/// <returns>统计信息字符串</returns>
|
|
public static string GetStatistics(string windowName)
|
|
{
|
|
if (!windowStats.ContainsKey(windowName))
|
|
{
|
|
return $"{windowName}: 无统计数据";
|
|
}
|
|
|
|
var stats = windowStats[windowName];
|
|
var timeSinceLastRefresh = DateTime.Now - stats.LastRefreshTime;
|
|
var successRate = ((stats.RefreshCount - stats.ErrorCount) * 100.0 / Math.Max(stats.RefreshCount, 1));
|
|
var runTime = DateTime.Now - stats.StartTime;
|
|
|
|
return $"{windowName}: 总刷新{stats.RefreshCount}, 错误{stats.ErrorCount}, 成功率{successRate:F1}%, " +
|
|
$"数据查询{stats.DataRetrievalCount}, 空数据{stats.EmptyDataCount}, " +
|
|
$"上次刷新{timeSinceLastRefresh.TotalMinutes:F1}分钟前, 运行{runTime.TotalHours:F1}小时";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有窗体的统计信息
|
|
/// </summary>
|
|
/// <returns>所有统计信息</returns>
|
|
public static string GetAllStatistics()
|
|
{
|
|
if (windowStats.Count == 0)
|
|
{
|
|
return "无监控数据";
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("=== 窗体刷新监控统计 ===");
|
|
foreach (var kvp in windowStats)
|
|
{
|
|
sb.AppendLine(GetStatistics(kvp.Key));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |