Log.cs
15.5 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using SqlSugar;
using System.Collections.Concurrent;
using System.IO;
using System.Windows;
namespace RCS.WinClient.Common
{
public class Log
{
//软件本身异常信息,便于程序调试
public string Path_Error { get; set; }
//日志信息:记录详细的信息流过程
public string Path_Message { get; set; }
public string Path_BusinessErr { get; set; }
//接口日志
public string Path_WebApi { get; set; }
public string Path_Sql { get; set; }
public string Path_AgvInfo { get; set; }
private readonly ConcurrentDictionary<string, ConcurrentQueue<string>> LogQueue = new ConcurrentDictionary<string, ConcurrentQueue<string>>();
public Log()
{
Path_Error = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\Err";
Path_Message = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\Message";
Path_BusinessErr = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\BusinessErr";
Path_WebApi = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\WebApi";
Path_Sql = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\Sql";
Path_AgvInfo = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\AgvInfo";
//日志的处理线程,先写入队列,然后用一个线程从队列读取后写入到文本,避免反复的去加载文件。
var logThread = new Thread(LogOperator)
{
IsBackground = true
};
logThread.Start();
}
/////<summary>
///// 目录检查
///// </summary>
//public void DirectoryCheck()
//{
// //异常信息
// if (!Directory.Exists(Path_Error)) Directory.CreateDirectory(Path_Error);
// //日志Log信息
// if (!Directory.Exists(Path_Message)) Directory.CreateDirectory(Path_Message);
// //Log信息
// if (!Directory.Exists(Path_BusinessErr)) Directory.CreateDirectory(Path_BusinessErr);
// //WebApi信息
// if (!Directory.Exists(Path_WebApi)) Directory.CreateDirectory(Path_WebApi);
// // SQL信息
// if (!Directory.Exists(Path_Sql)) Directory.CreateDirectory(Path_Sql);
// //电量信息
// if (!Directory.Exists(Path_AgvVoltage)) Directory.CreateDirectory(Path_AgvVoltage);
//}
///<summary>
/// 目录检查
/// </summary>
public void DirectoryCheck(string filePath)
{
//异常信息
if (filePath.StartsWith(Path_Error) && !Directory.Exists(Path_Error))
{
Directory.CreateDirectory(Path_Error);
}
//日志Log信息
if (filePath.StartsWith(Path_Message) && !Directory.Exists(Path_Message))
{
Directory.CreateDirectory(Path_Message);
}
//业务错误信息
if (filePath.StartsWith(Path_BusinessErr) && !Directory.Exists(Path_BusinessErr))
{
Directory.CreateDirectory(Path_BusinessErr);
}
//WebApi信息
if (filePath.StartsWith(Path_WebApi) && !Directory.Exists(Path_WebApi))
{
Directory.CreateDirectory(Path_WebApi);
}
// SQL信息
if (filePath.StartsWith(Path_Sql) && !Directory.Exists(Path_Sql))
{
Directory.CreateDirectory(Path_Sql);
}
//AGV上传信息
if (filePath.StartsWith(Path_AgvInfo) && !Directory.Exists(Path_AgvInfo))
{
Directory.CreateDirectory(Path_AgvInfo);
}
}
/// <summary>
/// 日志处理
/// </summary>
private void LogOperator()
{
try
{
while (true)
{
foreach (var item in LogQueue)
{
if (!item.Value.IsEmpty)
{
DirectoryCheck(item.Key);
using var sw = new StreamWriter(item.Key, true);
while (!item.Value.IsEmpty)
{
if (item.Value.TryDequeue(out string? logInfo))
{
sw.WriteLine(logInfo);
}
}
sw.Close();
}
}
Thread.Sleep(3000);
}
}
catch (Exception ex)
{
App.ExFile.MessageError("LogOperator", "创建日志处理线程失败:" + ex.Message.Replace(Environment.NewLine, ""));
var logThread = new Thread(LogOperator);
logThread.IsBackground = true;
logThread.Start();
}
}
/// <summary>
/// 把日志加入队列
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="Appended">是否追加文件信息</param>
/// <param name="LogInfo">信息内容</param>
private void AddLogInfo(string FilePath, string LogInfo)
{
try
{
if (!LogQueue.ContainsKey(FilePath))
{
LogQueue.TryAdd(FilePath, new ConcurrentQueue<string>());
}
LogQueue[FilePath].Enqueue(DateTime.Now.ToString("HH:mm:ss fff") + "==" + LogInfo);
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageWriter", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 向指定文件写入信息
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="Appended">是否追加文件信息</param>
/// <param name="LogInfo">信息内容</param>
public void MessageWriter(string FilePath, bool Appended, string LogInfo)
{
try
{
using (StreamWriter sw = new StreamWriter(FilePath, Appended))
{
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + "==" + LogInfo);
sw.Close();
}
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageWriter", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 日志信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageLog(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_Message + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageLog", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 业务处理异常信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageBusinessErr(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_BusinessErr + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
App.WinMain?.TBox_BussErrMsg.Dispatcher.BeginInvoke(new Action(() =>
{
App.WinMain.TBox_BussErrMsg.AppendText(DateTime.Now.ToString("HH:mm:ss fff") + "::" + fileName + "::" + Message + "\n");
if (App.WinMain.TBox_BussErrMsg.LineCount > 100)
{
App.WinMain.TBox_BussErrMsg.Clear();
}
}));
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageLog", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// 错误信息
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageError(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_Error + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
if (!pathName.Contains("Hide"))
{
App.WinMain?.TBox_EMessage.Dispatcher.BeginInvoke(new Action(() =>
{
App.WinMain.TBox_EMessage.AppendText(DateTime.Now.ToString("HH:mm:ss fff") + "::" + fileName + "::" + Message + "\n");
App.WinMain.TBox_EMessage.PageDown();
if (App.WinMain.TBox_EMessage.LineCount > 100)
{
App.WinMain.TBox_EMessage.Clear();
}
}));
}
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageError", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// WebApi接口报错信息
/// </summary>
/// <param name="fileName"></param>
/// <param name="Message"></param>
public void MessageWebApi(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_WebApi + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageWebApi", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// SQL日志信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessageSql(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_Sql + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
//App.WinMain.TBox_EMessage.Dispatcher.BeginInvoke(new Action(() =>
//{
// App.WinMain.TBox_EMessage.AppendText(DateTime.Now.ToString("HH:mm:ss fff") + "::" + fileName + "::" + Message + "\n");
// App.WinMain.TBox_EMessage.PageDown();
// if (App.WinMain.TBox_EMessage.LineCount > 100)
// {
// App.WinMain.TBox_EMessage.Clear();
// }
//}));
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageSql", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// AGV上传日志记录
/// </summary>
/// <param name="fileName"></param>
/// <param name="Message"></param>
public void MessageAgvInfo(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_AgvInfo + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
}
catch (Exception ex)
{
App.ExFile.MessageError("MessageAgvInfo", ex.Message.Replace(Environment.NewLine, ""));
}
}
/// <summary>
/// PLC日志信息
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="Message">信息内容</param>
public void MessagePLC(string fileName, string Message, bool distinct = true)
{
try
{
//DirectoryCheck();
string pathName = App.ExFile.Path_Message + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
if (LogQueue.ContainsKey(pathName) && distinct)
{
if (LogQueue[pathName].Any(t => t.IndexOf(Message) == 14))
{
return;
}
}
//MessageWriter(pathName, true, Message);
AddLogInfo(pathName, Message);
App.WinMain?.TBox_EMessage.Dispatcher.BeginInvoke(new Action(() =>
{
App.WinMain.TBox_EMessage.AppendText(DateTime.Now.ToString("HH:mm:ss fff") + "::" + fileName + "::" + Message + "\n");
App.WinMain.TBox_EMessage.PageDown();
if (App.WinMain.TBox_EMessage.LineCount > 100)
{
App.WinMain.TBox_EMessage.Clear();
}
}));
}
catch (Exception ex)
{
App.ExFile.MessageError("MessagePLC", ex.Message.Replace(Environment.NewLine, ""));
}
}
}
}