From f4e081a09676ea5bb7dfdc5e91221129bbd6ddf6 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 15 Aug 2024 19:13:21 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/ConsoleLogger.cs | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index f0f7ec9..8285571 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -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; @@ -74,7 +75,7 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu }; ConsoleMultilineMessage($""" {message} - {tag}{exception.GetType().Name}: {exception.Message} + {tag}{BuildExceptionTreeText(exception)} """, formatter); } } @@ -95,6 +96,42 @@ private static void ConsoleMultilineMessage(string message, Func + /// 以树形结构构建异常信息。 + /// + /// 在异常信息前的标签。 + /// 异常。 + /// 树形结构的异常信息。 + private static string BuildExceptionTreeText(Exception exception) + { + var builder = new StringBuilder(); + BuildExceptionTreeTextRecursively(builder, exception); + return builder.ToString(); + + static void BuildExceptionTreeTextRecursively(StringBuilder builder, Exception exception, int depth = 0) + { + if (depth is not 0) + { + builder + .Append(' ', depth * 2 + 2) + .Append('└') + .Append(exception.InnerException is not null ? '┬' : '─'); + } + builder.AppendLine($"{exception.GetType().Name}: {exception.Message}"); + if (exception is AggregateException ae) + { + foreach (var innerException in ae.InnerExceptions) + { + BuildExceptionTreeTextRecursively(builder, innerException, depth + 1); + } + } + else if (exception.InnerException is { } innerException) + { + BuildExceptionTreeTextRecursively(builder, innerException, depth + 1); + } + } + } + /// /// 高于或等于此级别的日志才会被记录。 /// From d5fced43089dfafbb1c3f2f1821626d3edfa88b7 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 15 Aug 2024 19:30:38 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=AE=8C=E6=95=B4=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=85=8D=E5=BE=97?= =?UTF-8?q?=E5=8E=BB=E6=8B=86=E5=88=86=E5=86=85=E9=83=A8=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=BF=98=E4=B8=8D=E8=83=BD=E5=BE=88=E5=A5=BD=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/ConsoleLogger.cs | 38 +------------------ 1 file changed, 1 insertion(+), 37 deletions(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index 8285571..d944cb5 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -75,7 +75,7 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu }; ConsoleMultilineMessage($""" {message} - {tag}{BuildExceptionTreeText(exception)} + {tag}{exception} """, formatter); } } @@ -96,42 +96,6 @@ private static void ConsoleMultilineMessage(string message, Func - /// 以树形结构构建异常信息。 - /// - /// 在异常信息前的标签。 - /// 异常。 - /// 树形结构的异常信息。 - private static string BuildExceptionTreeText(Exception exception) - { - var builder = new StringBuilder(); - BuildExceptionTreeTextRecursively(builder, exception); - return builder.ToString(); - - static void BuildExceptionTreeTextRecursively(StringBuilder builder, Exception exception, int depth = 0) - { - if (depth is not 0) - { - builder - .Append(' ', depth * 2 + 2) - .Append('└') - .Append(exception.InnerException is not null ? '┬' : '─'); - } - builder.AppendLine($"{exception.GetType().Name}: {exception.Message}"); - if (exception is AggregateException ae) - { - foreach (var innerException in ae.InnerExceptions) - { - BuildExceptionTreeTextRecursively(builder, innerException, depth + 1); - } - } - else if (exception.InnerException is { } innerException) - { - BuildExceptionTreeTextRecursively(builder, innerException, depth + 1); - } - } - } - /// /// 高于或等于此级别的日志才会被记录。 /// From 642a15c75c30be898866d90eb765e18683a376b2 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 15 Aug 2024 19:31:40 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=AD=89=E7=BA=A7=E6=97=A5=E5=BF=97=E7=9A=84=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dotnetCampus.Logger/Writers/ConsoleLogger.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index d944cb5..cfd15a2 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -1,6 +1,5 @@ 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; @@ -141,14 +140,14 @@ private void ClearAndMoveToLastLine(int repeatCount) } private const string Reset = C.Reset; - private const string DebugText = F.White; - private const string TraceText = F.BrightBlack; + private const string TraceText = F.Magenta; + private const string DebugText = F.BrightBlack; private const string InformationText = F.Green + D.Bold; private const string WarningText = F.Yellow; private const string ErrorText = F.BrightRed; private const string CriticalText = F.Red; - private static string TraceTag => $"{B.Black}{F.BrightBlack}[{DateTime.Now:HH:mm:ss.fff}]{Reset}"; + private static string TraceTag => $"{B.Magenta}{F.White}[{DateTime.Now:HH:mm:ss.fff}]{Reset}"; private static string DebugTag => $"{B.BrightBlack}{F.White}[{DateTime.Now:HH:mm:ss.fff}]{Reset}"; private static string InformationTag => $"{B.Green}{F.Black}[{DateTime.Now:HH:mm:ss.fff}]{Reset}"; private static string WarningTag => $"{B.Yellow}{F.Black}[{DateTime.Now:HH:mm:ss.fff}]{Reset}"; From 79614d395b3947cc21a586a32637d0041d045ed9 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 15 Aug 2024 19:57:14 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/Helpers/TagFilterManager.cs | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs index 4eec834..7708251 100644 --- a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs +++ b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace dotnetCampus.Logging.Writers.Helpers; @@ -109,54 +110,58 @@ internal bool IsTagEnabled(string text) /// 命令行参数。 public static TagFilterManager? FromCommandLineArgs(string[] args) { + if (!TryGetCommandLineValue(args, LogTagParameterName, out var value)) + { + return null; + } + HashSet anyFilterTags = []; HashSet includingFilterTags = []; HashSet excludingFilterTags = []; - for (var i = 0; i < args.Length; i++) + var filterTags = value.Split([',', ';', ' ']); + foreach (var tag in filterTags) { - if (args[i] != LogTagParameterName || i + 1 >= args.Length) - { - continue; - } - - var filterTags = args[i + 1].Split([',', ';', ' ']); - foreach (var tag in filterTags) + if (tag.StartsWith("-", StringComparison.Ordinal)) { - if (tag.StartsWith("-", StringComparison.Ordinal)) - { #if NET8_0_OR_GREATER - excludingFilterTags.Add(tag[1..]); + excludingFilterTags.Add(tag[1..]); #else - excludingFilterTags.Add(tag.Substring(1)); + excludingFilterTags.Add(tag.Substring(1)); #endif - } - else if (tag.StartsWith("+", StringComparison.Ordinal)) - { + } + else if (tag.StartsWith("+", StringComparison.Ordinal)) + { #if NET8_0_OR_GREATER - includingFilterTags.Add(tag[1..]); + includingFilterTags.Add(tag[1..]); #else - includingFilterTags.Add(tag.Substring(1)); + includingFilterTags.Add(tag.Substring(1)); #endif - } - else - { - anyFilterTags.Add(tag); - } } - - return new TagFilterManager + else { - AnyFilterTags = anyFilterTags.ToImmutableHashSet(), - IncludingFilterTags = includingFilterTags.ToImmutableHashSet(), - ExcludingFilterTags = excludingFilterTags.ToImmutableHashSet(), - }; + anyFilterTags.Add(tag); + } } return new TagFilterManager { - AnyFilterTags = [], - IncludingFilterTags = [], - ExcludingFilterTags = [], + AnyFilterTags = anyFilterTags.ToImmutableHashSet(), + IncludingFilterTags = includingFilterTags.ToImmutableHashSet(), + ExcludingFilterTags = excludingFilterTags.ToImmutableHashSet(), }; } + + private static bool TryGetCommandLineValue(string[] args, string parameterName, [NotNullWhen(true)] out string? value) + { + for (var i = 0; i < args.Length; i++) + { + if (string.Equals(args[i], parameterName, StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) + { + value = args[i + 1]; + return true; + } + } + value = null; + return false; + } } From 19af1888375dd11009a5a24216e075e7ca036e33 Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 15 Aug 2024 19:59:43 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=81=B0=E8=89=B2=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E9=9A=BE=E7=9C=8B=E6=B8=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dotnetCampus.Logger/Writers/ConsoleLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs index cfd15a2..24cc3ad 100644 --- a/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs +++ b/src/dotnetCampus.Logger/Writers/ConsoleLogger.cs @@ -141,7 +141,7 @@ private void ClearAndMoveToLastLine(int repeatCount) private const string Reset = C.Reset; private const string TraceText = F.Magenta; - private const string DebugText = F.BrightBlack; + private const string DebugText = F.White; private const string InformationText = F.Green + D.Bold; private const string WarningText = F.Yellow; private const string ErrorText = F.BrightRed;