using SqlSugar.DbConvert;
|
using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using static Tiger.Model.MQTT;
|
|
namespace Tiger.Model
|
{
|
/// <summary>
|
/// 调试
|
/// </summary>
|
public class TraceDebug
|
{
|
public TraceDebug(StackFrame frame)
|
{
|
var info = new TraceInfo("Begin Trace");
|
info.File = frame?.GetFileName();
|
info.Function = frame?.GetMethod().DeclaringType.FullName + "." + frame?.GetMethod().Name;
|
info.Line = frame?.GetFileLineNumber() ?? default;
|
TraceInfos.Add(info);
|
}
|
public TraceDebug(StackFrame frame, string title) : this(frame)
|
{
|
Title = title;
|
}
|
|
/// <summary>
|
/// 调试标题
|
/// </summary>
|
public string Title { get; set; } = "Trace Information";
|
/// <summary>
|
/// 调试信息列表
|
/// </summary>
|
public List<TraceInfo> TraceInfos { get; set; } = new List<TraceInfo>();
|
/// <summary>
|
/// 开始记录时间
|
/// </summary>
|
public DateTime BeginTime { get; set; } = DateTime.Now;
|
/// <summary>
|
/// 持续时间(秒)
|
/// </summary>
|
public double Duration { get; set; } = 0;
|
public override string ToString() => $"========== {Title} ==========\r\n{string.Join("\r\n", TraceInfos.Select(q => " " + q.ToString()))}\r\n========== Total Elapsed Time: {Duration:0.000}s ==========";
|
public string ToDetailString() => $"========== {Title} ==========\r\n{string.Join("\r\n", TraceInfos.Select(q => " " + q.ToDetailString()))}\r\n========== Total Elapsed Time: {Duration:0.000}s ==========";
|
/// <summary>
|
/// 添加调试信息
|
/// </summary>
|
public TraceDebug Log(StackFrame frame, string msg, Action<TraceInfo> doSomthing = null)
|
{
|
var f = new StackFrame(true);
|
var info = new TraceInfo(msg);
|
Duration = (info.ActionTime - BeginTime).TotalSeconds;
|
info.File = frame?.GetFileName();
|
info.Function = frame?.GetMethod().DeclaringType.FullName + "." + frame?.GetMethod().Name;
|
info.Line = frame?.GetFileLineNumber() ?? default;
|
info.Index = TraceInfos.Max(q => q.Index) + 1;
|
info.Duration = (info.ActionTime - TraceInfos.Max(q => q.ActionTime)).TotalSeconds;
|
//string w = null; w.ToString();
|
TraceInfos.Add(info);
|
doSomthing?.Invoke(info);
|
return this;
|
}
|
}
|
|
/// <summary>
|
/// 调试信息
|
/// </summary>
|
public class TraceInfo
|
{
|
public TraceInfo()
|
{
|
}
|
public TraceInfo(string message)
|
{
|
Message = message;
|
}
|
/// <summary>
|
/// 序号
|
/// </summary>
|
public int Index { get; set; } = 0;
|
/// <summary>
|
/// 调试文件
|
/// </summary>
|
public string File { get; set; } = "Anonymous";
|
/// <summary>
|
/// 调试方法
|
/// </summary>
|
public string Function { get; set; } = "Anonymous";
|
/// <summary>
|
/// 行号
|
/// </summary>
|
public int Line { get; set; } = 0;
|
/// <summary>
|
/// 信息
|
/// </summary>
|
public string Message { get; set; }
|
/// <summary>
|
/// 执行时间
|
/// </summary>
|
public DateTime ActionTime { get; set; } = DateTime.Now;
|
/// <summary>
|
/// 持续时间(秒)
|
/// </summary>
|
public double Duration { get; set; } = 0;
|
public override string ToString() => $"{ActionTime:yyyy/MM/dd HH:mm:ss.fff} > Trace[{Index}](took {Duration:0.000}s) : {Message}.";
|
public string ToDetailString() => $"{ActionTime:yyyy/MM/dd HH:mm:ss.fff} > Trace[{Index}](took {Duration:0.000}s) : {Message}.\r\n\tRun at {Function} in {File}:line {Line}";
|
}
|
}
|