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 @@ - + - - + + - - - + + + 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"; 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) diff --git a/src/WeihanLi.Common/Helpers/CommandLineParser.cs b/src/WeihanLi.Common/Helpers/CommandLineParser.cs index 34b67f2c..408d79a6 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; @@ -91,28 +92,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 bool 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) 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; } }