CLI: Change console log output format

This commit is contained in:
genteure 2022-07-07 21:33:46 +08:00
parent b1f740b69e
commit f4fd1c6d1c
2 changed files with 16 additions and 8 deletions

View File

@ -27,6 +27,7 @@
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" /> <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" /> <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.2.0" /> <PackageReference Include="Serilog.Exceptions" Version="8.2.0" />
<PackageReference Include="Serilog.Expressions" Version="3.4.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" /> <PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

View File

@ -29,7 +29,7 @@ using Serilog.Events;
using Serilog.Exceptions; using Serilog.Exceptions;
using Serilog.Filters; using Serilog.Filters;
using Serilog.Formatting.Compact; using Serilog.Formatting.Compact;
using Serilog.Sinks.SystemConsole.Themes; using Serilog.Templates;
namespace BililiveRecorder.Cli namespace BililiveRecorder.Cli
{ {
@ -422,11 +422,9 @@ namespace BililiveRecorder.Cli
var matchMicrosoft = Matching.FromSource("Microsoft"); var matchMicrosoft = Matching.FromSource("Microsoft");
ConsoleTheme theme = OperatingSystem.IsWindows() && string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("WT_SESSION")) var ansiColorSupport = !OperatingSystem.IsWindows() || !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("WT_SESSION"));
? SystemConsoleTheme.Literate
: AnsiConsoleTheme.Code;
return new LoggerConfiguration() var builder = new LoggerConfiguration()
.MinimumLevel.Verbose() .MinimumLevel.Verbose()
.Enrich.WithProcessId() .Enrich.WithProcessId()
.Enrich.WithThreadId() .Enrich.WithThreadId()
@ -443,7 +441,6 @@ namespace BililiveRecorder.Cli
x.FileCreationTime, x.FileCreationTime,
x.FileModificationTime, x.FileModificationTime,
}) })
.WriteTo.Console(restrictedToMinimumLevel: logLevel, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{RoomId}] {Message:lj}{NewLine}{Exception}", theme: theme)
.WriteTo.Logger(sl => .WriteTo.Logger(sl =>
{ {
sl sl
@ -457,8 +454,18 @@ namespace BililiveRecorder.Cli
.Filter.ByIncludingOnly(matchMicrosoft) .Filter.ByIncludingOnly(matchMicrosoft)
.WriteTo.File(new CompactJsonFormatter(), logFilePathMicrosoft, restrictedToMinimumLevel: logFileLevel, shared: true, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true) .WriteTo.File(new CompactJsonFormatter(), logFilePathMicrosoft, restrictedToMinimumLevel: logFileLevel, shared: true, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
; ;
}) });
.CreateLogger();
if (ansiColorSupport)
{
builder.WriteTo.Console(new ExpressionTemplate("[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}]{#if RoomId is not null} [{RoomId}]{#end} {@m}{#if ExceptionDetail is not null}\n [{ExceptionDetail['Type']}]: {ExceptionDetail['Message']}{#end}\n", theme: Serilog.Templates.Themes.TemplateTheme.Code), logLevel);
}
else
{
builder.WriteTo.Console(restrictedToMinimumLevel: logLevel, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] [{RoomId}] {Message:lj}{NewLine}{Exception}");
}
return builder.CreateLogger();
} }
public abstract class SharedArguments public abstract class SharedArguments