Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

c# - Is it possible to elegantly configure Serilog with if-statements?

My Serilog configuration code looks like this:

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();

I'd like to change this configuration at runtime, unfortunately because Serilog uses a "fluent" style of API it makes it somewhat messy. For example, if I want to enable or disable console and file logging at runtime:

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();

...which is not exactly elegant.

I know I could add my own If extension method (but I'm not keen on extending an existing API design like that, even if it does look prettier):

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;
}

What alternatives exist to toggle different Serilog options at runtime? Are there any approaches that can be used with other "Fluent" APIs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Serilog 2.9.0 introduces conditional sinks. Using .WriteTo.Conditional you specify the condition that defines if the sink will be written to or not.

e.g.

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();
// ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...