Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serilog ignores dotnet logging configuration #254

Open
vars-ecco opened this issue Jun 20, 2024 · 2 comments
Open

Serilog ignores dotnet logging configuration #254

vars-ecco opened this issue Jun 20, 2024 · 2 comments

Comments

@vars-ecco
Copy link

Hello,

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

builder.AddFilter<SerilogLoggerProvider>(null, LogLevel.Trace);

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
Copy link
Member

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.

@vars-ecco
Copy link
Author

Hi, thanks for the response,

Let's leave this ticket as the tracking issue.

Ok, feel free to close when needed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants