From 58ce875b4d7d5c3774c241bec2d787cc24b31cd3 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 10:37:54 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=BC=96=E5=86=99=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=8D=E5=AE=89=E5=85=A8=E7=9A=84=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LoggerSample.MainApp.csproj | 2 +- samples/LoggerSample.MainApp/Program.cs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/samples/LoggerSample.MainApp/LoggerSample.MainApp.csproj b/samples/LoggerSample.MainApp/LoggerSample.MainApp.csproj index 7788b98..c2e7e96 100644 --- a/samples/LoggerSample.MainApp/LoggerSample.MainApp.csproj +++ b/samples/LoggerSample.MainApp/LoggerSample.MainApp.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net8.0 preferReference diff --git a/samples/LoggerSample.MainApp/Program.cs b/samples/LoggerSample.MainApp/Program.cs index d3684d8..ea757ab 100644 --- a/samples/LoggerSample.MainApp/Program.cs +++ b/samples/LoggerSample.MainApp/Program.cs @@ -1,4 +1,6 @@ -using dotnetCampus.Logging.Attributes; +using System; +using System.Threading.Tasks; +using dotnetCampus.Logging.Attributes; using dotnetCampus.Logging.Configurations; using dotnetCampus.Logging.Writers; @@ -31,6 +33,16 @@ public static void Main(string[] args) .AddBridge(LoggerBridgeLinker.Default) .Build() .IntoGlobalStaticLog(); + + Run(); + } + + private static void Run() + { + Parallel.For(0, 0x00010000, i => + { + Log.Debug($"[TEST] {DateTime.Now:HH:mm:ss}"); + }); } } From 019c6fba45d554ac0ef02811ddff175ace24c118 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 11:31:29 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BD=BF=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=EF=BC=8C=E4=BD=BF=E7=94=A8=20Builder=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=88=9B=E5=BB=BA=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/LoggerSample.MainApp/Program.cs | 17 +-- src/dotnetCampus.Logger/LoggerBuilder.cs | 6 +- .../Writers/ConsoleLogger.cs | 105 ++++++++------- .../Writers/ConsoleLoggerBuilder.cs | 84 ++++++++++++ .../Writers/Helpers/ICoreLogWriter.cs | 123 ++++++++++++++++++ .../Writers/Helpers/TagFilterManager.cs | 3 + .../Writers/LogWritingThreadMode.cs | 25 ++++ 7 files changed, 305 insertions(+), 58 deletions(-) create mode 100644 src/dotnetCampus.Logger/Writers/ConsoleLoggerBuilder.cs create mode 100644 src/dotnetCampus.Logger/Writers/Helpers/ICoreLogWriter.cs create mode 100644 src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs diff --git a/samples/LoggerSample.MainApp/Program.cs b/samples/LoggerSample.MainApp/Program.cs index ea757ab..d650f93 100644 --- a/samples/LoggerSample.MainApp/Program.cs +++ b/samples/LoggerSample.MainApp/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; using dotnetCampus.Logging.Attributes; using dotnetCampus.Logging.Configurations; @@ -23,26 +24,26 @@ public static void Main(string[] args) { LogLevel = LogLevel.Debug, }) - .AddWriter(new ConsoleLogger - { - // Options = new ConsoleLoggerOptions - // { - // IncludeScopes = true, - // }, - }) + .AddConsoleLogger(b => b + .WithThreadSafe(LogWritingThreadMode.ProducerConsumer) + .FilterConsoleTagsFromCommandLineArgs(args)) .AddBridge(LoggerBridgeLinker.Default) .Build() .IntoGlobalStaticLog(); Run(); + Thread.Sleep(5000); } private static void Run() { - Parallel.For(0, 0x00010000, i => + Log.Debug("[TEST] 开始"); + Parallel.For(0, 0x00004000, i => { + Thread.Sleep(1); Log.Debug($"[TEST] {DateTime.Now:HH:mm:ss}"); }); + Log.Debug("[TEST] 完成"); } } diff --git a/src/dotnetCampus.Logger/LoggerBuilder.cs b/src/dotnetCampus.Logger/LoggerBuilder.cs index 3fcc2cb..945e32c 100644 --- a/src/dotnetCampus.Logger/LoggerBuilder.cs +++ b/src/dotnetCampus.Logger/LoggerBuilder.cs @@ -1,15 +1,13 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using dotnetCampus.Logging.Bridges; using dotnetCampus.Logging.Configurations; -using dotnetCampus.Logging.Writers; namespace dotnetCampus.Logging; /// /// 辅助创建日志记录器的构建器。 /// -public class LoggerBuilder +public sealed class LoggerBuilder { private LogOptions? _options; private readonly List _writers = []; diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index 24cc3ad..0f03c51 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -16,18 +16,30 @@ public class ConsoleLogger : ILogger private int _isCursorMovementEnabled = 3; private readonly RepeatLoggerDetector _repeat; - private TagFilterManager? _tagFilterManager; + + internal ConsoleLogger(ICoreLogWriter coreWriter, TagFilterManager? tagManager) + { + _repeat = new RepeatLoggerDetector(ClearAndMoveToLastLine); + CoreWriter = coreWriter; + TagManager = tagManager; + } /// /// 高于或等于此级别的日志才会被记录。 /// - public LogLevel Level { get; set; } + public LogLevel Level { get; init; } - public ConsoleLogger() - { - _repeat = new(ClearAndMoveToLastLine); - } + /// + /// 最终日志写入器。 + /// + private ICoreLogWriter CoreWriter { get; } + + /// + /// 管理控制台日志的标签过滤。 + /// + private TagFilterManager? TagManager { get; } + /// public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (logLevel < Level) @@ -36,7 +48,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } var message = formatter(state, exception); - if (_tagFilterManager?.IsTagEnabled(message) is false) + if (TagManager?.IsTagEnabled(message) is false) { return; } @@ -53,7 +65,14 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except }); } - private void LogCore(LogLevel logLevel, Exception? exception, string message, Func formatter) + /// + /// 记录日志。在必要的情况下会保证线程安全。 + /// + /// + /// + /// + /// + private void LogCore(LogLevel logLevel, Exception? exception, string message, Func formatter) => CoreWriter.Do(() => { if (_repeat.RepeatOrResetLastLog(logLevel, message, exception) is var count and > 1) { @@ -77,9 +96,15 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu {tag}{exception} """, formatter); } - } + }); - private static void ConsoleMultilineMessage(string message, Func formatter, bool forceSingleLine = false) + /// + /// 记录多行日志。 + /// + /// + /// + /// + private void ConsoleMultilineMessage(string message, Func formatter, bool forceSingleLine = false) { if (forceSingleLine || !message.Contains('\n')) { @@ -96,46 +121,34 @@ private static void ConsoleMultilineMessage(string message, Func - /// 高于或等于此级别的日志才会被记录。 - /// - public ConsoleLogger UseLevel(LogLevel level) - { - Level = level; - return this; - } - - /// - /// 从命令行参数中提取过滤标签。 + /// 清空当前行并移动光标到上一行。 /// - /// 命令行参数。 - public ConsoleLogger FilterConsoleTagsFromCommandLineArgs(string[] args) - { - _tagFilterManager = TagFilterManager.FromCommandLineArgs(args); - return this; - } - + /// 此移动光标,是因为日志已重复第几次。 private void ClearAndMoveToLastLine(int repeatCount) { - if (_isCursorMovementEnabled > 0 && repeatCount > 2) + if (_isCursorMovementEnabled <= 0 || repeatCount <= 2) { - try - { - var desiredY = Console.CursorTop - 1; - var y = Math.Clamp(desiredY, 0, Console.WindowHeight - 1); - Console.SetCursorPosition(0, y); - Console.Write(new string(' ', Console.WindowWidth)); - Console.SetCursorPosition(0, y); - } - catch (IOException) - { - // 日志记录时,如果无法移动光标,说明可能当前输出位置不在缓冲区内。 - // 如果多次尝试失败,则认为当前控制台缓冲区不支持光标移动,遂放弃。 - _isCursorMovementEnabled--; - } - catch (ArgumentException) - { - // 日志记录时,有可能已经移动到头了,就不要移动了。 - } + // 如果光标控制不可用,或者还没有重复次数,则不尝试移动光标。 + return; + } + + try + { + var desiredY = Console.CursorTop - 1; + var y = Math.Clamp(desiredY, 0, Console.WindowHeight - 1); + Console.SetCursorPosition(0, y); + Console.Write(new string(' ', Console.WindowWidth)); + Console.SetCursorPosition(0, y); + } + catch (IOException) + { + // 日志记录时,如果无法移动光标,说明可能当前输出位置不在缓冲区内。 + // 如果多次尝试失败,则认为当前控制台缓冲区不支持光标移动,遂放弃。 + _isCursorMovementEnabled--; + } + catch (ArgumentException) + { + // 日志记录时,有可能已经移动到头了,就不要移动了。 } } diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLoggerBuilder.cs b/src/dotnetCampus.Logger/Writers/ConsoleLoggerBuilder.cs new file mode 100644 index 0000000..5907290 --- /dev/null +++ b/src/dotnetCampus.Logger/Writers/ConsoleLoggerBuilder.cs @@ -0,0 +1,84 @@ +using System; +using dotnetCampus.Logging.Writers.Helpers; + +namespace dotnetCampus.Logging.Writers; + +/// +/// 辅助创建控制台日志记录器的构建器。 +/// +public sealed class ConsoleLoggerBuilder +{ + private TagFilterManager? _tagFilterManager; + private ICoreLogWriter _coreWriter = new NotThreadSafeLogWriter(); + + /// + /// 高于或等于此级别的日志才会被记录。 + /// + public LogLevel Level { get; set; } + + /// + /// 高于或等于此级别的日志才会被记录。 + /// + public ConsoleLoggerBuilder WithLevel(LogLevel level) + { + Level = level; + return this; + } + + /// + /// 指定控制台日志的线程安全模式。 + /// + /// 线程安全模式。 + /// 构造器模式。 + /// 线程安全模式不支持。 + public ConsoleLoggerBuilder WithThreadSafe(LogWritingThreadMode threadMode) + { + _coreWriter = threadMode switch + { + LogWritingThreadMode.NotThreadSafe => new NotThreadSafeLogWriter(), + LogWritingThreadMode.Lock => new LockLogWriter(), + LogWritingThreadMode.ProducerConsumer => new ProducerConsumerLogWriter(), + _ => throw new ArgumentOutOfRangeException(nameof(threadMode)), + }; + return this; + } + + /// + /// 从命令行参数中提取过滤标签,使得控制台日志支持过滤标签行为。 + /// + /// 命令行参数。 + /// 构造器模式。 + public ConsoleLoggerBuilder FilterConsoleTagsFromCommandLineArgs(string[] args) + { + _tagFilterManager = TagFilterManager.FromCommandLineArgs(args); + return this; + } + + /// + /// 创建控制台日志记录器。 + /// + /// 控制台日志记录器。 + internal ConsoleLogger Build() => new(_coreWriter, _tagFilterManager) + { + Level = Level, + }; +} + +/// +/// 辅助创建控制台日志记录器。 +/// +public static class ConsoleLoggerBuilderExtensions +{ + /// + /// 添加控制台日志记录器。 + /// + /// 日志构建器。 + /// 配置控制台日志记录器。 + /// 日志构建器。 + public static LoggerBuilder AddConsoleLogger(this LoggerBuilder builder, Action configure) + { + var consoleLoggerBuilder = new ConsoleLoggerBuilder(); + configure(consoleLoggerBuilder); + return builder.AddWriter(consoleLoggerBuilder.Build()); + } +} diff --git a/src/dotnetCampus.Logger/Writers/Helpers/ICoreLogWriter.cs b/src/dotnetCampus.Logger/Writers/Helpers/ICoreLogWriter.cs new file mode 100644 index 0000000..4d28ac1 --- /dev/null +++ b/src/dotnetCampus.Logger/Writers/Helpers/ICoreLogWriter.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Concurrent; +using System.Threading.Tasks; + +namespace dotnetCampus.Logging.Writers.Helpers; + +/// +/// 提供各种不同线程安全方式的最终日志写入功能。 +/// +internal interface ICoreLogWriter +{ + /// + /// 写入日志。 + /// + /// 日志消息。为 时不写入。 + void Write(string? message); + + /// + /// 插入一个动作。 + /// + /// 动作。 + void Do(Action action); +} + +/// +/// 不处理线程安全问题的日志写入器。 +/// +internal sealed class NotThreadSafeLogWriter : ICoreLogWriter +{ + /// + public void Write(string? message) + { + if (message is not null) + { + Console.WriteLine(message); + } + } + + /// + public void Do(Action action) + { + action(); + } +} + +/// +/// 使用锁来保证线程安全的日志写入器。 +/// +internal sealed class LockLogWriter : ICoreLogWriter +{ + private readonly object _lock = new(); + + /// + public void Write(string? message) + { + if (message is not null) + { + lock (_lock) + { + Console.WriteLine(message); + } + } + } + + /// + public void Do(Action action) + { + lock (_lock) + { + action(); + } + } +} + +/// +/// 使用生产者消费者模式的日志写入器。 +/// +internal sealed class ProducerConsumerLogWriter : ICoreLogWriter +{ + private readonly BlockingCollection _queue = new(); + + /// + /// 创建 的新实例,并启动消费线程。 + /// + public ProducerConsumerLogWriter() + { + new Task(Consume, TaskCreationOptions.LongRunning).Start(); + } + + /// + public void Write(string? message) + { + if (message is not null) + { + _queue.Add(message); + } + } + + /// + public void Do(Action action) + { + _queue.Add(action); + } + + /// + /// 消费队列中的元素。 + /// + private void Consume() + { + foreach (var item in _queue.GetConsumingEnumerable()) + { + switch (item) + { + case string message: + Console.WriteLine(message); + break; + case Action action: + action(); + break; + } + } + } +} diff --git a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs index 3b3f2ba..c6b5ecd 100644 --- a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs +++ b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs @@ -5,6 +5,9 @@ namespace dotnetCampus.Logging.Writers.Helpers; +/// +/// 管理控制台日志的标签过滤。 +/// internal class TagFilterManager { public const string LogTagParameterName = "--log-console-tags"; diff --git a/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs b/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs new file mode 100644 index 0000000..c540747 --- /dev/null +++ b/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs @@ -0,0 +1,25 @@ +namespace dotnetCampus.Logging.Writers; + +/// +/// 表示如何管理日志的写入线程。 +/// +public enum LogWritingThreadMode +{ + /// + /// 在哪个线程调用日志,就在哪个线程写入日志。不处理线程安全问题。 + /// + NotThreadSafe, + + /// + /// 使用锁来保证线程安全。 + /// + Lock, + + /// + /// 使用生产者消费者模式,将日志写入到队列中,由后台线程消费。 + /// + /// + /// 截至目前,使用此方法难以保证在程序退出时,所有日志都能被写入。 + /// + ProducerConsumer, +} From dbfc58369538a7ce4a69dd650f520d5dae96f320 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 11:37:00 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BD=BF=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=99=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BB=8E=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/ConsoleLogger.cs | 13 ++++++++++ .../Writers/LogWritingThreadMode.cs | 25 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index 0f03c51..53b1396 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -8,6 +8,9 @@ namespace dotnetCampus.Logging.Writers; +/// +/// 在控制台输出日志的日志记录器。 +/// public class ConsoleLogger : ILogger { /// @@ -17,6 +20,16 @@ public class ConsoleLogger : ILogger private readonly RepeatLoggerDetector _repeat; + /// + /// 创建一个 的新实例。 + /// + /// 指定控制台日志的线程安全模式。 + /// Main 方法的参数。 + public ConsoleLogger(LogWritingThreadMode threadMode = LogWritingThreadMode.NotThreadSafe, string[]? mainArgs = null) + : this(threadMode.CreateCoreLogWriter(), TagFilterManager.FromCommandLineArgs(mainArgs ?? [])) + { + } + internal ConsoleLogger(ICoreLogWriter coreWriter, TagFilterManager? tagManager) { _repeat = new RepeatLoggerDetector(ClearAndMoveToLastLine); diff --git a/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs b/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs index c540747..072db49 100644 --- a/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs +++ b/src/dotnetCampus.Logger/Writers/LogWritingThreadMode.cs @@ -1,4 +1,7 @@ -namespace dotnetCampus.Logging.Writers; +using System; +using dotnetCampus.Logging.Writers.Helpers; + +namespace dotnetCampus.Logging.Writers; /// /// 表示如何管理日志的写入线程。 @@ -23,3 +26,23 @@ public enum LogWritingThreadMode /// ProducerConsumer, } + +/// +/// 包含 的扩展方法。 +/// +internal static class LogWritingThreadModeExtensions +{ + /// + /// 根据 创建对应的 实例。 + /// + /// 线程安全模式。 + /// 最终日志写入器。 + /// 当线程安全模式不支持时抛出。 + public static ICoreLogWriter CreateCoreLogWriter(this LogWritingThreadMode threadMode) => threadMode switch + { + LogWritingThreadMode.NotThreadSafe => new NotThreadSafeLogWriter(), + LogWritingThreadMode.Lock => new LockLogWriter(), + LogWritingThreadMode.ProducerConsumer => new ProducerConsumerLogWriter(), + _ => throw new ArgumentOutOfRangeException(nameof(threadMode)), + }; +} From 96d155af60f6f69ef18c7cdf2667ff8d10d0360a Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 11:43:53 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/LoggerSample.MainApp/Program.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/samples/LoggerSample.MainApp/Program.cs b/samples/LoggerSample.MainApp/Program.cs index d650f93..fec6569 100644 --- a/samples/LoggerSample.MainApp/Program.cs +++ b/samples/LoggerSample.MainApp/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using dotnetCampus.Logging.Attributes; @@ -37,13 +38,14 @@ public static void Main(string[] args) private static void Run() { - Log.Debug("[TEST] 开始"); + var stopwatch = Stopwatch.StartNew(); + Log.Debug($"[TEST] 开始 {stopwatch.ElapsedMilliseconds}ms"); Parallel.For(0, 0x00004000, i => { - Thread.Sleep(1); + Thread.Sleep(0); Log.Debug($"[TEST] {DateTime.Now:HH:mm:ss}"); }); - Log.Debug("[TEST] 完成"); + Log.Debug($"[TEST] 完成 {stopwatch.ElapsedMilliseconds}ms"); } } From 4bc04ff39e999b93870c42227cd682acef97ea69 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 11:46:13 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca61ee7..2cf5257 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,8 @@ internal static class LoggerStartup // 设置日志级别为 Debug。 .WithLevel(LogLevel.Debug) // 添加一个控制台日志写入器,这样控制台里就可以看到日志输出了。 - .AddWriter(new ConsoleLogger() + .AddConsoleLogger(b => b + .WithThreadSafe(LogWritingThreadMode.ProducerConsumer) .FilterConsoleTagsFromCommandLineArgs(args)) // 如果有一些库使用了本日志框架(使用源生成器,不带依赖的那种),那么可以通过这个方法将它们的日志桥接到本日志框架中。 .AddBridge(LoggerBridgeLinker.Default) From 573f89c174a8651b9ad6cad35d695a55867a6720 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 22 Aug 2024 11:56:02 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=9C=A8=E5=88=87=E6=8D=A2=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=89=8D=EF=BC=8C=E5=85=88=E6=8A=8A=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=BB=99=E6=8D=95=E8=8E=B7=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=97=A5=E5=BF=97=E6=97=B6=E9=97=B4=E4=B8=8E?= =?UTF-8?q?=E5=AE=9E=E9=99=85=E4=B8=8D=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/ConsoleLogger.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index 53b1396..db38b54 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -66,14 +66,20 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except return; } + var traceTag = TraceTag; + var debugTag = DebugTag; + var informationTag = InformationTag; + var warningTag = WarningTag; + var errorTag = ErrorTag; + var criticalTag = CriticalTag; LogCore(logLevel, exception, message, m => logLevel switch { - LogLevel.Trace => $"{TraceTag} {TraceText}{m}{Reset}", - LogLevel.Debug => $"{DebugTag} {DebugText}{m}{Reset}", - LogLevel.Information => $"{InformationTag} {InformationText}{m}{Reset}", - LogLevel.Warning => $"{WarningTag} {WarningText}{m}{Reset}", - LogLevel.Error => $"{ErrorTag} {ErrorText}{m}{Reset}", - LogLevel.Critical => $"{CriticalTag} {CriticalText}{m}{Reset}", + LogLevel.Trace => $"{traceTag} {TraceText}{m}{Reset}", + LogLevel.Debug => $"{debugTag} {DebugText}{m}{Reset}", + LogLevel.Information => $"{informationTag} {InformationText}{m}{Reset}", + LogLevel.Warning => $"{warningTag} {WarningText}{m}{Reset}", + LogLevel.Error => $"{errorTag} {ErrorText}{m}{Reset}", + LogLevel.Critical => $"{criticalTag} {CriticalText}{m}{Reset}", _ => null, }); }