一个基于 C# 的简单的线程安全日志模块

一个基于 C# 的简单的线程安全日志模块,它使用生产者 - 消费者模式,可以在 .NET Framework 和 .Net Core 中使用。

Github 地址:LogConsumer

使用

  1. LogConsumer.cs 添加到你的项目中
  2. LogConsumer.cs 中的 logFileName 修改为你的路径文件名
  3. 在需要输出日志的地方使用它
    1
    HuiHut.LogConsumer.LogConsumer.Instance.Write("your log content");

编译运行

  • 命令行

    1
    2
    dotnet build
    dotnet .\LogConsumer\bin\Debug\netcoreapp2.1\LogConsumer.dll
  • Visual Studio

    1. 打开 LogConsumer.sln
    2. 生成解决方案,运行测试

测试的日志文件示例

10 个线程,每个线程抛出 10 条日志的测试

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
Data       Time      Namespace          Class           Method    LogContent
----------------------------------------------------------------------------------
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 0
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 1
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 2
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 3
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 4
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 5
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 6
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 7
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 5 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 8
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 2 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 3 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 6 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 7 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] index = 9
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 0 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 1 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 9 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 8 ] Thread is finished.
2019-05-10 01:50:09 HuiHut.LogConsumer:LogConsumerTest.WriteLog [ 4 ] Thread is finished.