SystemLog.cs
3.98 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using NLog;
using System.Collections.Concurrent;
using System.Windows.Media;
namespace HHECS.DAQClient.Common
{
/// <summary>
/// 日志
/// </summary>
public class SystemLog
{
private static readonly SystemLog _instance = new();
private readonly ConcurrentQueue<LogModel> _queue = new();
private readonly Logger _nlog = LogManager.GetCurrentClassLogger();
/// <summary>
/// 日志队列包含的元素数
/// </summary>
public int Count => _queue.Count;
/// <summary>
/// 日志队列是否为空
/// </summary>
public bool IsEmpty => _queue.IsEmpty;
private SystemLog() { }
/// <summary>
/// 获取日志对象实例
/// </summary>
/// <returns></returns>
public static SystemLog GetInstance()
{
return _instance;
}
/// <summary>
/// 记录日志
/// </summary>
/// <param name="message">日志内容</param>
/// <param name="type">日志类型</param>
public void Log(string message, LogType type = LogType.Info)
{
_queue.Enqueue(new LogModel
{
Messages = message,
LogType = type,
});
if (type == LogType.Debug)
{
_nlog.Error(message);
}
else
{
_nlog.Info(message);
}
}
/// <summary>
/// 记录成功日志
/// </summary>
/// <param name="message">日志内容</param>
public void LogSuccess(string message)
{
Log(message, LogType.Success);
}
/// <summary>
/// 记录消息日志
/// </summary>
/// <param name="message">日志内容</param>
public void LogInfo(string message)
{
Log(message, LogType.Info);
}
/// <summary>
/// 记录警告日志
/// </summary>
/// <param name="message">日志内容</param>
public void LogWarning(string message)
{
Log(message, LogType.Warning);
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="message">日志内容</param>
public void LogError(string message)
{
Log(message, LogType.Error);
}
/// <summary>
/// 输出错误日志信息,并将日志保存至程序错误日志文件
/// </summary>
/// <param name="message">日志内容</param>
public void LogException(string message)
{
Log(message, LogType.Debug);
}
/// <summary>
/// 读取一条日志
/// </summary>
/// <returns></returns>
public LogModel GetLog()
{
_queue.TryDequeue(out LogModel log);
return log;
}
}
/// <summary>
/// 日志实体类,本地日志文件,不存储到数据库
/// </summary>
public class LogModel
{
public DateTime CreateTime { get; set; } = DateTime.Now;
public string Messages { get; set; } = string.Empty;
public LogType LogType { get; set; }
public SolidColorBrush ForeColor => LogType switch
{
LogType.Default => Brushes.Black,
LogType.Success => Brushes.Green,
LogType.Info => Brushes.Black,
LogType.Warning => Brushes.Orange,
LogType.Error => Brushes.OrangeRed,
LogType.Debug => Brushes.Red,
_ => Brushes.Black,
};
}
/// <summary>
/// 日志级别
/// </summary>
public enum LogType
{
/// <summary>
/// 默认
/// </summary>
Default,
Success,
Info,
Warning,
Error,
/// <summary>
/// 程序异常日志,Exception 错误信息请使用此类型
/// </summary>
Debug
}
}