7

I'm trying to configure Serilog for a Web API project in .NET 6.

Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(builder.Configuration)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .CreateLogger();

builder.Logging.ClearProviders();

builder.Logging.AddSerilog(Log.Logger);
//builder.Host.UseSerilog(Log.Logger);

What behavior difference is there between adding Serilog to the logging pipeline and setting Serilog as the logging provider? Should I call both methods?

user246392
  • 2,661
  • 11
  • 54
  • 96
  • 2
    Generally, AddXXX is for registering implementations, and UseXXX is for adding to the asp pipelines. If you are writing a console app, then you wouldn't need the UseXXX because that isn't an asp application. – Neil Oct 08 '22 at 14:03

2 Answers2

9

There is a huge difference.

The .AddSerilog() provider adds a Serilog provider as one of potentially many providers. With the following configuration, the Microsoft logger will first log to the Console provider, then to the Serilog provider:

.ConfigureLogging(logging => logging.AddConsole().AddSerilog())

The .UseSerilog() configures Serilog as the only provider. The following will send all logs to Serilog regardless of whether you've configured the logging pipeline:

.UseSerilog();

The difference really boils down to using Micosoft's pluggable model or using Serilog's pluggable model.

Typically you wouldn't use .AddSerilog() as the Serilog library is really intended to be used as the sole provider with one or more "sinks", but there may be cases where you need to log to a particular destination for which there exists a Micosoft ILogger and ILoggerProvider, but for which no Serilog sink exists (and you don't want to have to write it yourself). In such cases, you might choose to add Serilog as an additional provider.

Derek Greer
  • 15,454
  • 5
  • 45
  • 52
  • Can't we use `AddConsole()` followed by `UseSerilog()`. In that case there is no difference except the syntax – Sujoy Dec 23 '22 at 14:08
  • As stated, the UseSerilog() extension method _replaces_ the default logging provider. You can't use both together. To demonstrate, see https://dotnetfiddle.net/0E01VG – Derek Greer Dec 24 '22 at 17:36
-2

There is no difference, only syntax varies. However you need to ensure that if you read from configuration then there is no need to mention enrichers or files in the declaration, else you might end up creating two log file output:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()       // No need
    .WriteTo.Console()             // No need
    .WriteTo.File("Logs/Log.txt")  // No Need
    .CreateLogger();

With intellisense it is easier to define the configuration in code rather than in appsetting.json. As opposed to configuration you can detect error in this method quite easily. Hence better use :

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()       
    .WriteTo.Console()             
    .WriteTo.File("Logs/Log.txt")  
    .CreateLogger();
Sujoy
  • 1,051
  • 13
  • 26