是否可以使用if语句优雅地配置Serilog?

时间:2018-10-25 18:21:00

标签: c# fluent serilog

我的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一起使用?

2 个答案:

答案 0 :(得分:3)

我认为要使其优雅并仍在代码中进行,您必须扩展API并创建自己的扩展方法,以封装条件检查并使用正确的接收器和参数更新构建器。

类似

Log.Logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .Enrich.FromLogContext()
    .MinimumLevel.Warning()
    .WriteToConsoleIfEnabled()  // <---
    .WriteToFileIfEnabled()     // <---
    .CreateLogger();

另一方面,您是否考虑过改用Serilog.Settings.AppSettingsSerilog.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>

答案 1 :(得分:3)

Serilog 2.9.0引入了条件接收器。使用.WriteTo.Conditional,您可以指定条件,以定义是否将接收器写入。

例如

bool enableConsoleLogging = ...
bool enableFileLogging = ...

var builder = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .Enrich.FromLogContext()
    .MinimumLevel.Warning()
    .WriteTo.Conditional(evt => enableConsoleLogging, wt => wt.Console())
    .WriteTo.Conditional(evt => enableFileLogging, wt => wt.File(...));

Log.Logger = builder.CreateLogger();
// ...