Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当 WinExe 输出时,避免控制台日志输出乱码 #32

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/dotnetCampus.Logger/Writers/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using dotnetCampus.Logging.Writers.Helpers;
using C = dotnetCampus.Logging.Writers.ConsoleLoggerHelpers.ConsoleColors;
using B = dotnetCampus.Logging.Writers.ConsoleLoggerHelpers.ConsoleColors.Background;
Expand All @@ -16,9 +17,11 @@ public class ConsoleLogger : ILogger
/// <summary>
/// 控制台光标控制是否启用。目前可容纳的错误次数为 3 次,当降低到 0 次时,将不再尝试移动光标。
/// </summary>
private int _isCursorMovementEnabled = 3;
private int _isCursorMovementEnabled;

private readonly RepeatLoggerDetector _repeat;
private static bool _isConsoleOutput;
private static readonly TextWriter Out = GetStandardOutputWriter();

/// <summary>
/// 创建一个 <see cref="ConsoleLogger"/> 的新实例。
Expand All @@ -35,6 +38,9 @@ internal ConsoleLogger(ICoreLogWriter coreWriter, TagFilterManager? tagManager)
_repeat = new RepeatLoggerDetector(ClearAndMoveToLastLine);
CoreWriter = coreWriter;
TagManager = tagManager;
_isConsoleOutput = Out == Console.Out;
// 如果输出流是自己创建的,则不支持光标移动。
_isCursorMovementEnabled = _isConsoleOutput ? 3 : 0;
}

/// <summary>
Expand Down Expand Up @@ -105,7 +111,14 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu
{
if (_repeat.RepeatOrResetLastLog(logLevel, message, exception) is var count and > 1)
{
ConsoleMultilineMessage($"上述日志已重复 {count} 次", formatter, true);
if (_isConsoleOutput)
{
ConsoleMultilineMessage($"上述日志已重复 {count} 次", formatter, true);
}
else
{
ConsoleMultilineMessage(message, m => $"{formatter(m)}{F.BrightBlack} (重复 {count} 次){Reset}", true);
}
}
else if (exception is null)
{
Expand Down Expand Up @@ -157,7 +170,7 @@ internal static void SafeWriteLine(string? message)
{
try
{
Console.WriteLine(message);
Out.WriteLine(message);
}
catch (IOException)
{
Expand Down Expand Up @@ -198,6 +211,26 @@ private void ClearAndMoveToLastLine(int repeatCount)
}
}

/// <summary>
/// 获取标准输出的写入流。<br/>
/// 如果当前在控制台中输出,则使用控制台的输出流;否则创建一个 UTF-8 编码的标准输出流。
/// </summary>
/// <returns>文本写入流。</returns>
private static TextWriter GetStandardOutputWriter()
{
if (Console.OutputEncoding.CodePage is not 0)
{
return Console.Out;
}

var standardOutput = Console.OpenStandardOutput();
var writer = new StreamWriter(standardOutput, Encoding.UTF8)
{
AutoFlush = true,
};
return writer;
}

private const string Reset = C.Reset;
private const string TraceText = F.Magenta;
private const string DebugText = F.White;
Expand Down