serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
306 stars 97 forks source link

Serilog ignores dotnet logging configuration #254

Open vars-ecco opened 1 week ago

vars-ecco commented 1 week ago

Hello,

I would like to ask, what is the reason for this line

https://github.com/serilog/serilog-extensions-logging/blob/1e9f655aec4fa4335c68dcce64daac9a25f0011e/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs#L48

The thing is that I would like to use dotnet configuration for serilog as well, i.e. something like this


"Logging": {
  "LogLevel": {
    "Default": "None"
  },
  "Serilog": {
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

But that line creates a rule that overwrites the logging configuration.

What is a proper way to use serilog with dotnet configuration rules? Should I simply avoid using AddSerilog method and call builder.AddProvider(new SerilogLoggerProvider(logger)) instead?

nblumhardt commented 1 week ago

Hi! Yes, working around this with AddProvider(new ...) is probably the way to go.

This is an unfortunate remnant of the original ".NET Core 2" iteration of this library, before we implemented the UseSerilog() (logger factory) method of integration. Back at that time, having two logging pipelines with level configuration both impacting the final result was problematic.

If I could go back in time :-), in "provider mode" I'd now avoid doing this, and just leave MEL's filters alone when using Serilog as a provider rather than as the complete logging pipeline.

Serilog 4 introduces a new public CreateSink() API that can instantiate any Serilog sink independently of the Serilog pipeline. I've been wondering whether this might be a good route to replacing the current provider mechanism with something based on e.g.:

host.Logging.AddSink(writeTo => writeTo.File(...));

Behind the scenes, this would use LoggerSinkConfiguration.CreateSink() to turn the argument into an ILogEventSink, and a SerilogSinkLoggerProvider would be wrapped around the result.

JSON configuration support is still a bit of an unknown, though, so needs some more investigation. Let's leave this ticket as the tracking issue.