服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-11-28 d0fe891cac88bbd5087a0ee90a41421bbf0ec5c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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}";
    }
}