我的Serilog配置代码如下所示:
Log.Logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.MinimumLevel.Warning()
// .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
// .MinimumLevel.Override("System", LogEventLevel.Verbose)
// .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Verbose)
.WriteTo.Console( outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate )
// .WriteTo.File()
.CreateLogger();
我想在运行时更改此配置,不幸的是,因为Serilog使用“流利的”样式的API,这使它有些混乱.例如,如果我想在运行时启用或禁用控制台和文件记录:
Boolean enableConsoleLogging = ...
Boolean enableFileLogging = ...
LoggerConfiguration builder = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.MinimumLevel.Warning();
if( enableConsoleLogging )
{
builder = builder
.WriteTo.Console( outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate )
}
if( enableFileLogging )
{
builder = builder
.WriteTo.File( ... )
}
Log.Logger = builder.CreateLogger();
…并不十分优雅.
我知道我可以添加自己的If扩展方法(但我不希望扩展这样的现有API设计,即使看起来确实更漂亮):
Log.Logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.MinimumLevel.Warning()
.If( enableConsoleLogging, b => b.WriteTo.Console( outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate ) )
.If( enableFileLogging, b => b.WriteTo.File( ... ) )
.CreateLogger();
public static LoggerConfiguration If( this LoggerConfiguration cfg, Boolean test, Func<LoggerConfiguration,LoggerConfiguration> action )
{
if( test ) return action( cfg );
else return cfg;
}
有哪些替代方案可以在运行时切换不同的Serilog选项?是否可以与其他“ Fluent” API一起使用?
解决方法:
我认为,要使其优雅并仍在代码中进行,就必须扩展API并创建自己的扩展方法,以封装条件检查并使用正确的接收器和参数更新构建器.
就像是
Log.Logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.MinimumLevel.Warning()
.WriteToConsoleIfEnabled() // <---
.WriteToFileIfEnabled() // <---
.CreateLogger();
另一方面,您是否考虑过改用Serilog.Settings.AppSettings
或Serilog.Settings.Configuration
?代码中的配置变得更加简洁,您可以根据需要在配置文件中添加/删除接收器…
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger()
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="serilog:minimum-level" value="Verbose" />
<add key="serilog:using:Console" value="Serilog.Sinks.Console" />
<add key="serilog:write-to:Console" />
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\myapp-{Date}.txt" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />
<!-- //etc... -->
</appSettings>
</configuration>