Search code examples
asp.net-coreserilogasp.net-core-8serilog-aspnetcore

Registering Serilog with Logging.AddSerilog vs Services.AddSerilog


There are two ways to register Serilog.

The "old" way (shown in countless SO questions and tutorials):

//builder.Logging.ClearProviders();    // optional
builder.Logging.AddSerilog();

And the "new" way (shown in the current documentation):

builder.Services.AddSerilog();

Both are supported. How do they differ?


Solution

  • I found the answer in a related repo issue and blog post:

    • builder.Logging.AddSerilog adds Serilog as an additional provider to the logging pipeline
    • builder.Services.AddSerilog sets Serilog as the sole provider (thus the ClearProviders call is unnecessary)
    • builder.Host.UseSerilog forwards to builder.Services.AddSerilog; obsolete

    Typically one would want to use builder.Services.AddSerilog.