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 { /// /// 调试 /// 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; } /// /// 调试标题 /// public string Title { get; set; } = "Trace Information"; /// /// 调试信息列表 /// public List TraceInfos { get; set; } = new List(); /// /// 开始记录时间 /// public DateTime BeginTime { get; set; } = DateTime.Now; /// /// 持续时间(秒) /// 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 =========="; /// /// 添加调试信息 /// public TraceDebug Log(StackFrame frame, string msg, Action 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; } } /// /// 调试信息 /// public class TraceInfo { public TraceInfo() { } public TraceInfo(string message) { Message = message; } /// /// 序号 /// public int Index { get; set; } = 0; /// /// 调试文件 /// public string File { get; set; } = "Anonymous"; /// /// 调试方法 /// public string Function { get; set; } = "Anonymous"; /// /// 行号 /// public int Line { get; set; } = 0; /// /// 信息 /// public string Message { get; set; } /// /// 执行时间 /// public DateTime ActionTime { get; set; } = DateTime.Now; /// /// 持续时间(秒) /// 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}"; } }