@@ -16,18 +16,30 @@ public class ConsoleLogger : ILogger
16
16
private int _isCursorMovementEnabled = 3 ;
17
17
18
18
private readonly RepeatLoggerDetector _repeat ;
19
- private TagFilterManager ? _tagFilterManager ;
19
+
20
+ internal ConsoleLogger ( ICoreLogWriter coreWriter , TagFilterManager ? tagManager )
21
+ {
22
+ _repeat = new RepeatLoggerDetector ( ClearAndMoveToLastLine ) ;
23
+ CoreWriter = coreWriter ;
24
+ TagManager = tagManager ;
25
+ }
20
26
21
27
/// <summary>
22
28
/// 高于或等于此级别的日志才会被记录。
23
29
/// </summary>
24
- public LogLevel Level { get ; set ; }
30
+ public LogLevel Level { get ; init ; }
25
31
26
- public ConsoleLogger ( )
27
- {
28
- _repeat = new ( ClearAndMoveToLastLine ) ;
29
- }
32
+ /// <summary>
33
+ /// 最终日志写入器。
34
+ /// </summary>
35
+ private ICoreLogWriter CoreWriter { get ; }
36
+
37
+ /// <summary>
38
+ /// 管理控制台日志的标签过滤。
39
+ /// </summary>
40
+ private TagFilterManager ? TagManager { get ; }
30
41
42
+ /// <inheritdoc />
31
43
public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception ? exception , Func < TState , Exception ? , string > formatter )
32
44
{
33
45
if ( logLevel < Level )
@@ -36,7 +48,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
36
48
}
37
49
38
50
var message = formatter ( state , exception ) ;
39
- if ( _tagFilterManager ? . IsTagEnabled ( message ) is false )
51
+ if ( TagManager ? . IsTagEnabled ( message ) is false )
40
52
{
41
53
return ;
42
54
}
@@ -53,7 +65,14 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
53
65
} ) ;
54
66
}
55
67
56
- private void LogCore ( LogLevel logLevel , Exception ? exception , string message , Func < string , string ? > formatter )
68
+ /// <summary>
69
+ /// 记录日志。在必要的情况下会保证线程安全。
70
+ /// </summary>
71
+ /// <param name="logLevel"></param>
72
+ /// <param name="exception"></param>
73
+ /// <param name="message"></param>
74
+ /// <param name="formatter"></param>
75
+ private void LogCore ( LogLevel logLevel , Exception ? exception , string message , Func < string , string ? > formatter ) => CoreWriter . Do ( ( ) =>
57
76
{
58
77
if ( _repeat . RepeatOrResetLastLog ( logLevel , message , exception ) is var count and > 1 )
59
78
{
@@ -77,9 +96,15 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu
77
96
{ tag } { exception }
78
97
""" , formatter ) ;
79
98
}
80
- }
99
+ } ) ;
81
100
82
- private static void ConsoleMultilineMessage ( string message , Func < string , string ? > formatter , bool forceSingleLine = false )
101
+ /// <summary>
102
+ /// 记录多行日志。
103
+ /// </summary>
104
+ /// <param name="message"></param>
105
+ /// <param name="formatter"></param>
106
+ /// <param name="forceSingleLine"></param>
107
+ private void ConsoleMultilineMessage ( string message , Func < string , string ? > formatter , bool forceSingleLine = false )
83
108
{
84
109
if ( forceSingleLine || ! message . Contains ( '\n ' ) )
85
110
{
@@ -96,46 +121,34 @@ private static void ConsoleMultilineMessage(string message, Func<string, string?
96
121
}
97
122
98
123
/// <summary>
99
- /// 高于或等于此级别的日志才会被记录。
100
- /// </summary>
101
- public ConsoleLogger UseLevel ( LogLevel level )
102
- {
103
- Level = level ;
104
- return this ;
105
- }
106
-
107
- /// <summary>
108
- /// 从命令行参数中提取过滤标签。
124
+ /// 清空当前行并移动光标到上一行。
109
125
/// </summary>
110
- /// <param name="args">命令行参数。</param>
111
- public ConsoleLogger FilterConsoleTagsFromCommandLineArgs ( string [ ] args )
112
- {
113
- _tagFilterManager = TagFilterManager . FromCommandLineArgs ( args ) ;
114
- return this ;
115
- }
116
-
126
+ /// <param name="repeatCount">此移动光标,是因为日志已重复第几次。</param>
117
127
private void ClearAndMoveToLastLine ( int repeatCount )
118
128
{
119
- if ( _isCursorMovementEnabled > 0 && repeatCount > 2 )
129
+ if ( _isCursorMovementEnabled <= 0 || repeatCount <= 2 )
120
130
{
121
- try
122
- {
123
- var desiredY = Console . CursorTop - 1 ;
124
- var y = Math . Clamp ( desiredY , 0 , Console . WindowHeight - 1 ) ;
125
- Console . SetCursorPosition ( 0 , y ) ;
126
- Console . Write ( new string ( ' ' , Console . WindowWidth ) ) ;
127
- Console . SetCursorPosition ( 0 , y ) ;
128
- }
129
- catch ( IOException )
130
- {
131
- // 日志记录时,如果无法移动光标,说明可能当前输出位置不在缓冲区内。
132
- // 如果多次尝试失败,则认为当前控制台缓冲区不支持光标移动,遂放弃。
133
- _isCursorMovementEnabled -- ;
134
- }
135
- catch ( ArgumentException )
136
- {
137
- // 日志记录时,有可能已经移动到头了,就不要移动了。
138
- }
131
+ // 如果光标控制不可用,或者还没有重复次数,则不尝试移动光标。
132
+ return ;
133
+ }
134
+
135
+ try
136
+ {
137
+ var desiredY = Console . CursorTop - 1 ;
138
+ var y = Math . Clamp ( desiredY , 0 , Console . WindowHeight - 1 ) ;
139
+ Console . SetCursorPosition ( 0 , y ) ;
140
+ Console . Write ( new string ( ' ' , Console . WindowWidth ) ) ;
141
+ Console . SetCursorPosition ( 0 , y ) ;
142
+ }
143
+ catch ( IOException )
144
+ {
145
+ // 日志记录时,如果无法移动光标,说明可能当前输出位置不在缓冲区内。
146
+ // 如果多次尝试失败,则认为当前控制台缓冲区不支持光标移动,遂放弃。
147
+ _isCursorMovementEnabled -- ;
148
+ }
149
+ catch ( ArgumentException )
150
+ {
151
+ // 日志记录时,有可能已经移动到头了,就不要移动了。
139
152
}
140
153
}
141
154
0 commit comments