From dc5f3398d2f4a02941ecd58236db7bbc6bceff6e Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Mon, 1 Jul 2024 22:11:04 +0800 Subject: [PATCH 1/7] feat: add BooleanValue for CommandLineParser --- src/WeihanLi.Common/Helpers/CommandLineParser.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/WeihanLi.Common/Helpers/CommandLineParser.cs b/src/WeihanLi.Common/Helpers/CommandLineParser.cs index 34b67f2c..d59c8c35 100644 --- a/src/WeihanLi.Common/Helpers/CommandLineParser.cs +++ b/src/WeihanLi.Common/Helpers/CommandLineParser.cs @@ -91,28 +91,26 @@ public static IEnumerable ParseLine(string line, LineParseOptions? optio /// /// Get argument value from arguments /// - /// arguments - /// argument name to get value + /// argument name to get value /// default argument value when not found + /// arguments /// argument value [return: NotNullIfNotNull(nameof(defaultValue))] - [Obsolete("Please use Val instead")] - public static string? ArgValue(string[] args, string argumentName, string? defaultValue = default) + public static string? Val(string optionName, string? defaultValue = default, string[]? args = null) { - return GetOptionValueInternal(args, argumentName) ?? defaultValue; + return GetOptionValueInternal(args ?? Environment.GetCommandLineArgs(), optionName) ?? defaultValue; } /// - /// Get argument value from arguments + /// Get boolean argument value from arguments /// /// argument name to get value /// default argument value when not found /// arguments /// argument value - [return: NotNullIfNotNull(nameof(defaultValue))] - public static string? Val(string optionName, string? defaultValue = default, string[]? args = null) + public static string? BooleanVal(string optionName, bool defaultValue = default, string[]? args = null) { - return GetOptionValueInternal(args ?? Environment.GetCommandLineArgs(), optionName) ?? defaultValue; + return GetOptionValueInternal(args ?? Environment.GetCommandLineArgs(), optionName).ToBoolean(defaultValue); } private static string? GetOptionValueInternal(string[] args, string argumentName) From a55f507ba4fb3cecccb950f673f33bb167d0c9bd Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Mon, 1 Jul 2024 22:15:30 +0800 Subject: [PATCH 2/7] Update CommandLineParser.cs --- src/WeihanLi.Common/Helpers/CommandLineParser.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/WeihanLi.Common/Helpers/CommandLineParser.cs b/src/WeihanLi.Common/Helpers/CommandLineParser.cs index d59c8c35..cbb80a2b 100644 --- a/src/WeihanLi.Common/Helpers/CommandLineParser.cs +++ b/src/WeihanLi.Common/Helpers/CommandLineParser.cs @@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Text; +using WeihanLi.Extensions; namespace WeihanLi.Common.Helpers; From d8a8a1eee894006dd7b061e32fb0eb454cf03918 Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Mon, 1 Jul 2024 22:19:49 +0800 Subject: [PATCH 3/7] Update CommandLineParser.cs fix return value --- src/WeihanLi.Common/Helpers/CommandLineParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WeihanLi.Common/Helpers/CommandLineParser.cs b/src/WeihanLi.Common/Helpers/CommandLineParser.cs index cbb80a2b..408d79a6 100644 --- a/src/WeihanLi.Common/Helpers/CommandLineParser.cs +++ b/src/WeihanLi.Common/Helpers/CommandLineParser.cs @@ -109,7 +109,7 @@ public static IEnumerable ParseLine(string line, LineParseOptions? optio /// default argument value when not found /// arguments /// argument value - public static string? BooleanVal(string optionName, bool defaultValue = default, string[]? args = null) + public static bool BooleanVal(string optionName, bool defaultValue = default, string[]? args = null) { return GetOptionValueInternal(args ?? Environment.GetCommandLineArgs(), optionName).ToBoolean(defaultValue); } From f558700dff58631e1f172fc8c2a25c7636b9de88 Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Sun, 7 Jul 2024 23:55:31 +0800 Subject: [PATCH 4/7] bump package version --- build/version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/version.props b/build/version.props index 05276a2f..763604ba 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 1 0 - 66 + 67 $(VersionMajor).$(VersionMinor).$(VersionPatch) From 4754995036e2ceaed35d337101bc1309b27d6b6e Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Mon, 8 Jul 2024 22:11:23 +0800 Subject: [PATCH 5/7] feat: update DelegateLoggerProvider --- .../MicrosoftLoggingLoggerExtensions.cs | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs b/src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs index bb028454..87e8633c 100644 --- a/src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs +++ b/src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.DependencyInjection; using System.Collections.Concurrent; +using System.Diagnostics; using WeihanLi.Common; +using WeihanLi.Common.Helpers; using WeihanLi.Common.Logging; using WeihanLi.Common.Services; @@ -12,7 +14,45 @@ public sealed class DelegateLoggerProvider(Action { - Console.WriteLine(@$"[{level}][{category}] {msg}\n{exception}"); + var (foregroundColor, backgroundColor) = GetConsoleColorForLogLevel(level); + var levelText = GetLogLevelText(level); + var dateTime = DateTimeOffset.Now; + var message = @$"[{levelText}][{category}] {dateTime} {msg}"; + if (exception is not null) + { + message = $"{message}{Environment.NewLine}{exception}"; + } + + ConsoleHelper.WriteLineWithColor(message, foregroundColor, backgroundColor); + if (level is LogLevel.Trace) + { + Trace.WriteLine(message); + } + + return; + + static (ConsoleColor? ForegroundColor, ConsoleColor? BackgroundColor) GetConsoleColorForLogLevel(LogLevel logLevel) + => logLevel switch + { + LogLevel.Trace or LogLevel.Debug => (ConsoleColor.DarkGray, ConsoleColor.Black), + LogLevel.Information => (ConsoleColor.DarkGreen, ConsoleColor.Black), + LogLevel.Warning => (ConsoleColor.Yellow, ConsoleColor.Black), + LogLevel.Error => (ConsoleColor.Black, ConsoleColor.DarkRed), + LogLevel.Critical => (ConsoleColor.White, ConsoleColor.DarkRed), + _ => (null, null) + }; + + static string GetLogLevelText(LogLevel logLevel) + => logLevel switch + { + LogLevel.Trace => "trce", + LogLevel.Debug => "dbug", + LogLevel.Information => "info", + LogLevel.Warning => "warn", + LogLevel.Error => "fail", + LogLevel.Critical => "crit", + _ => logLevel.ToString().ToLowerInvariant() + }; }); private readonly ConcurrentDictionary _loggers = new(); @@ -35,20 +75,14 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except logAction.Invoke(categoryName, logLevel, exception, msg); } - public bool IsEnabled(LogLevel logLevel) - { - return true; - } + public bool IsEnabled(LogLevel logLevel) => true; #if NET7_0_OR_GREATER IDisposable? #else IDisposable #endif - ILogger.BeginScope(TState state) - { - return NullScope.Instance; - } + ILogger.BeginScope(TState state) => NullScope.Instance; } } From 8ee70b2a6e25e7f5dc4ec168e7e592972c125090 Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Wed, 10 Jul 2024 08:18:11 +0800 Subject: [PATCH 6/7] upgrade dependencies --- Directory.Packages.props | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 14bd4b07..cf845708 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,7 +5,7 @@ 6.0.0 7.0.0 8.0.0 - 9.0.0-preview.5.24306.7 + 9.0.0-preview.6.24327.7 @@ -20,20 +20,20 @@ - + - - + + - - - + + + From 12df91895b91c204c63b448ff3c872ca681ba73c Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Wed, 10 Jul 2024 08:35:59 +0800 Subject: [PATCH 7/7] build: use CommandLineParser.BooleanVal --- build/build.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/build.cs b/build/build.cs index 271c2d31..036de8a7 100644 --- a/build/build.cs +++ b/build/build.cs @@ -3,8 +3,8 @@ var target = CommandLineParser.Val("target", "Default", args); var apiKey = CommandLineParser.Val("apiKey", "", args); -var stable = CommandLineParser.Val("stable", null, args).ToBoolean(); -var noPush = CommandLineParser.Val("noPush", null, args).ToBoolean(); +var stable = CommandLineParser.BooleanVal("stable", false, args); +var noPush = CommandLineParser.BooleanVal("noPush", false, args); var branchName = EnvHelper.Val("BUILD_SOURCEBRANCHNAME", "local"); var solutionPath = "./WeihanLi.Common.sln";