Skip to content

Commit

Permalink
Merge pull request #210 from WeihanLi/dev
Browse files Browse the repository at this point in the history
1.0.67
  • Loading branch information
WeihanLi authored Jul 10, 2024
2 parents aef1f25 + 12df918 commit ff72b7d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
14 changes: 7 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0'">6.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">7.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">8.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.5.24306.7</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.6.24327.7</ExtensionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
Expand All @@ -20,20 +20,20 @@
<PackageVersion Include="Serilog" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="FluentAssertions" Version="6.6.0" />
<PackageVersion Include="Moq" Version="[4.18.4]" />
<PackageVersion Include="xunit" Version="2.8.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Xunit.DependencyInjection" Version="8.7.1" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.6" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
<PackageVersion Include="Dapper" Version="2.1.44" />
Expand Down
4 changes: 2 additions & 2 deletions build/build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>66</VersionPatch>
<VersionPatch>67</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
17 changes: 8 additions & 9 deletions src/WeihanLi.Common/Helpers/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Text;
using WeihanLi.Extensions;

namespace WeihanLi.Common.Helpers;

Expand Down Expand Up @@ -91,28 +92,26 @@ public static IEnumerable<string> ParseLine(string line, LineParseOptions? optio
/// <summary>
/// Get argument value from arguments
/// </summary>
/// <param name="args">arguments</param>
/// <param name="argumentName">argument name to get value</param>
/// <param name="optionName">argument name to get value</param>
/// <param name="defaultValue">default argument value when not found</param>
/// <param name="args">arguments</param>
/// <returns>argument value</returns>
[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;
}

/// <summary>
/// Get argument value from arguments
/// Get boolean argument value from arguments
/// </summary>
/// <param name="optionName">argument name to get value</param>
/// <param name="defaultValue">default argument value when not found</param>
/// <param name="args">arguments</param>
/// <returns>argument value</returns>
[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)
Expand Down
52 changes: 43 additions & 9 deletions src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,7 +14,45 @@ public sealed class DelegateLoggerProvider(Action<string, LogLevel, Exception?,
{
internal static ILoggerProvider Default { get; } = new DelegateLoggerProvider((category, level, exception, msg) =>
{
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<string, DelegateLogger> _loggers = new();
Expand All @@ -35,20 +75,14 @@ public void Log<TState>(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>(TState state)
{
return NullScope.Instance;
}
ILogger.BeginScope<TState>(TState state) => NullScope.Instance;
}
}

Expand Down

0 comments on commit ff72b7d

Please sign in to comment.