LogBase.cs
19.3 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
using HslCommunication.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace HslCommunication.LogNet
{
/// <summary>
/// 日志存储类的基类,提供一些基础的服务
/// </summary>
/// <remarks>
/// 基于此类可以实现任意的规则的日志存储规则,欢迎大家补充实现,本组件实现了3个日志类
/// <list type="number">
/// <item>单文件日志类 <see cref="LogNetSingle"/></item>
/// <item>根据文件大小的类 <see cref="LogNetFileSize"/></item>
/// <item>根据时间进行存储的类 <see cref="LogNetDateTime"/></item>
/// </list>
/// </remarks>
public abstract class LogNetBase : IDisposable
{
#region Constructor
/// <summary>
/// 实例化一个日志对象
/// </summary>
public LogNetBase()
{
m_fileSaveLock = new SimpleHybirdLock();
m_simpleHybirdLock = new SimpleHybirdLock();
m_WaitForSave = new Queue<HslMessageItem>();
filtrateKeyword = new List<string>();
filtrateLock = new SimpleHybirdLock();
}
#endregion
#region Private Member
private HslMessageDegree m_messageDegree = HslMessageDegree.DEBUG; // 默认的存储规则
private Queue<HslMessageItem> m_WaitForSave; // 待存储数据的缓存
private SimpleHybirdLock m_simpleHybirdLock; // 缓存列表的锁
private int m_SaveStatus = 0; // 存储状态
private List<string> filtrateKeyword; // 需要过滤的存储对象
private SimpleHybirdLock filtrateLock; // 过滤列表的锁
#endregion
#region Protect Member
/// <summary>
/// 文件存储的锁
/// </summary>
protected SimpleHybirdLock m_fileSaveLock; // 文件的锁
#endregion
#region Event Handle
/// <summary>
/// 在存储到文件的时候将会触发的事件
/// </summary>
public event EventHandler<HslEventArgs> BeforeSaveToFile = null;
private void OnBeforeSaveToFile(HslEventArgs args)
{
BeforeSaveToFile?.Invoke(this, args);
}
#endregion
#region Public Member
/// <summary>
/// 日志存储模式,1:单文件,2:按大小存储,3:按时间存储
/// </summary>
public int LogSaveMode { get; protected set; }
#endregion
#region Log Method
/// <summary>
/// 写入一条调试信息
/// </summary>
/// <param name="text"></param>
public void WriteDebug(string text)
{
WriteDebug(string.Empty, text);
}
/// <summary>
/// 写入一条调试信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本内容</param>
public void WriteDebug(string keyWord, string text)
{
RecordMessage(HslMessageDegree.DEBUG, keyWord, text);
}
/// <summary>
/// 写入一条普通信息
/// </summary>
/// <param name="text">文本内容</param>
public void WriteInfo(string text)
{
WriteInfo(string.Empty, text);
}
/// <summary>
/// 写入一条普通信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本内容</param>
public void WriteInfo(string keyWord, string text)
{
RecordMessage(HslMessageDegree.INFO, keyWord, text);
}
/// <summary>
/// 写入一条警告信息
/// </summary>
/// <param name="text">文本内容</param>
public void WriteWarn(string text)
{
WriteWarn(string.Empty, text);
}
/// <summary>
/// 写入一条警告信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本内容</param>
public void WriteWarn(string keyWord, string text)
{
RecordMessage(HslMessageDegree.WARN, keyWord, text);
}
/// <summary>
/// 写入一条错误消息
/// </summary>
/// <param name="text">文本内容</param>
public void WriteError(string text)
{
WriteError(string.Empty, text);
}
/// <summary>
/// 写入一条错误消息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本内容</param>
public void WriteError(string keyWord, string text)
{
RecordMessage(HslMessageDegree.ERROR, keyWord, text);
}
/// <summary>
/// 写入一条致命错误信息
/// </summary>
/// <param name="text">文本内容</param>
public void WriteFatal(string text)
{
WriteFatal(string.Empty, text);
}
/// <summary>
/// 写入一条致命错误信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本内容</param>
public void WriteFatal(string keyWord, string text)
{
RecordMessage(HslMessageDegree.FATAL, keyWord, text);
}
/// <summary>
/// 写入一条异常信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="ex">异常信息</param>
public void WriteException(string keyWord, Exception ex)
{
WriteException(keyWord, string.Empty, ex);
}
/// <summary>
/// 写入一条异常信息
/// </summary>
/// <param name="keyWord">关键字</param>
/// <param name="text">内容</param>
/// <param name="ex">异常</param>
public void WriteException(string keyWord, string text, Exception ex)
{
RecordMessage(HslMessageDegree.FATAL, keyWord, LogNetManagment.GetSaveStringFromException(text, ex));
}
/// <summary>
/// 记录一条自定义的消息
/// </summary>
/// <param name="degree">消息的等级</param>
/// <param name="keyWord">关键字</param>
/// <param name="text">文本</param>
public void RecordMessage(HslMessageDegree degree, string keyWord, string text)
{
WriteToFile(degree, keyWord, text);
}
/// <summary>
/// 写入一条解释性的消息,不需要带有回车键
/// </summary>
/// <param name="description">解释性的文本</param>
public void WriteDescrition(string description)
{
if (string.IsNullOrEmpty(description)) return;
// 和上面的文本之间追加一行空行
StringBuilder stringBuilder = new StringBuilder("\u0002");
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("\u0002/");
int count = 118 - CalculateStringOccupyLength(description);
if (count >= 8)
{
int count_1 = (count - 8) / 2;
AppendCharToStringBuilder(stringBuilder, '*', count_1);
stringBuilder.Append(" ");
stringBuilder.Append(description);
stringBuilder.Append(" ");
if (count % 2 == 0)
{
AppendCharToStringBuilder(stringBuilder, '*', count_1);
}
else
{
AppendCharToStringBuilder(stringBuilder, '*', count_1 + 1);
}
}
else if (count >= 2)
{
int count_1 = (count - 2) / 2;
AppendCharToStringBuilder(stringBuilder, '*', count_1);
stringBuilder.Append(description);
if (count % 2 == 0)
{
AppendCharToStringBuilder(stringBuilder, '*', count_1);
}
else
{
AppendCharToStringBuilder(stringBuilder, '*', count_1 + 1);
}
}
else
{
stringBuilder.Append(description);
}
stringBuilder.Append("/");
stringBuilder.Append(Environment.NewLine);
RecordMessage(HslMessageDegree.None, string.Empty, stringBuilder.ToString());
//ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolSaveText), stringBuilder.ToString());
}
/// <summary>
/// 写入一条任意字符
/// </summary>
/// <param name="text">内容</param>
public void WriteAnyString(string text)
{
RecordMessage(HslMessageDegree.None, string.Empty, text);
}
/// <summary>
/// 写入一条换行符
/// </summary>
public void WriteNewLine()
{
RecordMessage(HslMessageDegree.None, string.Empty, "\u0002" + Environment.NewLine);
//ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolSaveText), "\u0002" + Environment.NewLine);
}
/// <summary>
/// 设置日志的存储等级,高于该等级的才会被存储
/// </summary>
/// <param name="degree">消息等级</param>
public void SetMessageDegree(HslMessageDegree degree)
{
m_messageDegree = degree;
}
#endregion
#region Filtrate Keyword
/// <summary>
/// 过滤指定的关键字存储
/// </summary>
/// <param name="keyWord">关键字</param>
public void FiltrateKeyword(string keyWord)
{
filtrateLock.Enter();
if (!filtrateKeyword.Contains(keyWord))
{
filtrateKeyword.Add(keyWord);
}
filtrateLock.Leave();
}
#endregion
#region File Write
private void WriteToFile(HslMessageDegree degree, string keyWord, string text)
{
// 过滤事件
if (degree <= m_messageDegree)
{
// 需要记录数据
HslMessageItem item = GetHslMessageItem(degree, keyWord, text);
AddItemToCache(item);
}
}
private void AddItemToCache(HslMessageItem item)
{
m_simpleHybirdLock.Enter();
m_WaitForSave.Enqueue(item);
m_simpleHybirdLock.Leave();
StartSaveFile();
}
private void StartSaveFile()
{
if (Interlocked.CompareExchange(ref m_SaveStatus, 1, 0) == 0)
{
//启动存储
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolSaveFile), null);
}
}
private HslMessageItem GetAndRemoveLogItem()
{
HslMessageItem result = null;
m_simpleHybirdLock.Enter();
result = m_WaitForSave.Count > 0 ? m_WaitForSave.Dequeue() : null;
m_simpleHybirdLock.Leave();
return result;
}
private void ThreadPoolSaveFile(object obj)
{
// 获取需要存储的日志
HslMessageItem current = GetAndRemoveLogItem();
// 进入文件操作的锁
m_fileSaveLock.Enter();
// 获取要存储的文件名称
string LogSaveFileName = GetFileSaveName();
if (!string.IsNullOrEmpty(LogSaveFileName))
{
// 保存
StreamWriter sw = null;
try
{
sw = new StreamWriter(LogSaveFileName, true, Encoding.UTF8);
while (current != null)
{
// 触发事件
OnBeforeSaveToFile(new HslEventArgs() { HslMessage = current });
// 检查是否需要真的进行存储
bool isSave = true;
filtrateLock.Enter();
isSave = !filtrateKeyword.Contains(current.KeyWord);
filtrateLock.Leave();
// 检查是否被设置为强制不存储
if (current.Cancel) isSave = false;
// 如果需要存储的就过滤掉
if (isSave)
{
sw.Write(HslMessageFormate(current));
sw.Write(Environment.NewLine);
}
current = GetAndRemoveLogItem();
}
}
catch (Exception ex)
{
AddItemToCache(current);
AddItemToCache(new HslMessageItem()
{
Degree = HslMessageDegree.FATAL,
Text = LogNetManagment.GetSaveStringFromException("LogNetSelf", ex),
});
}
finally
{
sw?.Dispose();
}
}
// 释放锁
m_fileSaveLock.Leave();
Interlocked.Exchange(ref m_SaveStatus, 0);
// 再次检测锁是否释放完成
if (m_WaitForSave.Count > 0)
{
StartSaveFile();
}
}
private string HslMessageFormate(HslMessageItem hslMessage)
{
StringBuilder stringBuilder = new StringBuilder();
if (hslMessage.Degree != HslMessageDegree.None)
{
stringBuilder.Append("\u0002");
stringBuilder.Append("[");
stringBuilder.Append(LogNetManagment.GetDegreeDescription(hslMessage.Degree));
stringBuilder.Append("] ");
stringBuilder.Append(hslMessage.Time.ToString("yyyy-MM-dd HH:mm:ss.fff"));
stringBuilder.Append(" thread:[");
stringBuilder.Append(hslMessage.ThreadId.ToString("D2"));
stringBuilder.Append("] ");
if (!string.IsNullOrEmpty(hslMessage.KeyWord))
{
stringBuilder.Append(hslMessage.KeyWord);
stringBuilder.Append(" : ");
}
}
stringBuilder.Append(hslMessage.Text);
return stringBuilder.ToString();
}
private void ThreadPoolSaveText(object obj)
{
// 进入文件操作的锁
m_fileSaveLock.Enter();
//获取要存储的文件名称
string LogSaveFileName = GetFileSaveName();
if (!string.IsNullOrEmpty(LogSaveFileName))
{
// 保存
StreamWriter sw = null;
try
{
sw = new StreamWriter(LogSaveFileName, true, Encoding.UTF8);
string str = obj as string;
sw.Write(str);
}
catch (Exception ex)
{
AddItemToCache(new HslMessageItem()
{
Degree = HslMessageDegree.FATAL,
Text = LogNetManagment.GetSaveStringFromException("LogNetSelf", ex),
});
}
finally
{
sw?.Dispose();
}
}
// 释放锁
m_fileSaveLock.Leave();
}
#endregion
#region Helper Method
/// <summary>
/// 获取要存储的文件的名称
/// </summary>
/// <returns>完整的文件路径信息,带文件名</returns>
protected virtual string GetFileSaveName()
{
return string.Empty;
}
/// <summary>
/// 返回检查的路径名称,将会包含反斜杠
/// </summary>
/// <param name="filePath">路径信息</param>
/// <returns>检查后的结果对象</returns>
protected string CheckPathEndWithSprit(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (!filePath.EndsWith(@"\"))
{
return filePath + @"\";
}
}
return filePath;
}
private HslMessageItem GetHslMessageItem(HslMessageDegree degree, string keyWord, string text)
{
return new HslMessageItem()
{
KeyWord = keyWord,
Degree = degree,
Text = text,
ThreadId = Thread.CurrentThread.ManagedThreadId,
};
}
private int CalculateStringOccupyLength(string str)
{
if (string.IsNullOrEmpty(str)) return 0;
int result = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 0x4e00 && str[i] <= 0x9fbb)
{
result += 2;
}
else
{
result += 1;
}
}
return result;
}
private void AppendCharToStringBuilder(StringBuilder sb, char c, int count)
{
for (int i = 0; i < count; i++)
{
sb.Append(c);
}
}
#endregion
#region IDisposable Support
private bool disposedValue = false; // 要检测冗余调用
/// <summary>
/// 释放资源
/// </summary>
/// <param name="disposing">是否初次调用</param>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: 释放托管状态(托管对象)。
m_simpleHybirdLock.Dispose();
m_WaitForSave.Clear();
m_fileSaveLock.Dispose();
}
// TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
// TODO: 将大型字段设置为 null。
disposedValue = true;
}
}
// TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
// ~LogNetBase() {
// // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
// Dispose(false);
// }
// 添加此代码以正确实现可处置模式。
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
Dispose(true);
// TODO: 如果在以上内容中替代了终结器,则取消注释以下行。
// GC.SuppressFinalize(this);
}
#endregion
}
}