Skip to content

Commit 4fe49cd

Browse files
committed
在 WinExe 时直接输出,在 Exe 时输出重复
1 parent 471c0a4 commit 4fe49cd

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/DotNetCampus.Logger/Writers/ConsoleLogger.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public class ConsoleLogger : ILogger
1818
/// <summary>
1919
/// 控制台光标控制是否启用。
2020
/// </summary>
21-
private bool _isCursorMovementEnabled;
22-
21+
private readonly bool _isCursorMovementEnabled;
2322
private readonly RepeatLoggerDetector _repeat;
2423
private static bool _isConsoleOutput;
2524
private static readonly TextWriter Out = GetStandardOutputWriter();
@@ -115,7 +114,7 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu
115114
{
116115
if (_isConsoleOutput)
117116
{
118-
ConsoleMultilineMessage($"上述日志已重复 {count} 次", formatter, true);
117+
ConsoleMultilineMessage($"{F.BrightBlack}上述日志已重复 {count}{Reset}", formatter, true);
119118
}
120119
else
121120
{
@@ -185,13 +184,12 @@ internal static void SafeWriteLine(string? message)
185184
/// 清空当前行并移动光标到上一行。
186185
/// </summary>
187186
/// <param name="repeatCount">此移动光标,是因为日志已重复第几次。</param>
188-
/// <returns>返回 <see langword="true"/> 表示成功移动了光标,返回 <see langword="false"/> 表示未能成功移动光标。</returns>
189-
private bool ClearAndMoveToLastLine(int repeatCount)
187+
private void ClearAndMoveToLastLine(int repeatCount)
190188
{
191189
if (!_isCursorMovementEnabled)
192190
{
193191
// 如果光标控制不可用,或者还没有重复次数,则不尝试移动光标。
194-
return false;
192+
return;
195193
}
196194

197195
var width = Console.WindowWidth;
@@ -209,8 +207,6 @@ private bool ClearAndMoveToLastLine(int repeatCount)
209207
#else
210208
Out.Write($"{cursorPreviousLine}{new string(' ', width)}{cursorHorizontalAbsolute0}");
211209
#endif
212-
213-
return true;
214210
}
215211

216212
public static int Clamp(int value, int min, int max)

src/DotNetCampus.Logger/Writers/Helpers/RepeatLoggerDetector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace DotNetCampus.Logging.Writers.Helpers;
55

6-
internal class RepeatLoggerDetector(Func<int, bool> whenRepeated)
6+
internal class RepeatLoggerDetector(Action<int> whenRepeated)
77
{
88
private static volatile int _lastSameItemCount;
99
private static LogItem? _lastItem;
@@ -22,8 +22,8 @@ internal int RepeatOrResetLastLog(LogLevel level, string message, Exception? exc
2222
// 2. 当本次为第 1 次重复时,首次输出
2323
// 3. 当本次为第 2 次重复时,输出“重复日志行”
2424
// 4. 当本次为第 >2 次重复时,将向上移动光标,顶替掉上次的“重复日志行”
25-
var supportRepeatOutput = whenRepeated(count);
26-
return supportRepeatOutput ? count : 1;
25+
whenRepeated(count);
26+
return count;
2727
}
2828
else
2929
{

0 commit comments

Comments
 (0)