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

Log filtering seems to be ignored due to override #147

Closed
dpoole73 opened this issue Jul 18, 2019 · 1 comment
Closed

Log filtering seems to be ignored due to override #147

dpoole73 opened this issue Jul 18, 2019 · 1 comment

Comments

@dpoole73
Copy link

I've been trying to use the AddSerilog() extension method to add Serilog logging in my builder but found that any filtering I apply is overridden in the extension method, for example:

services.AddLogging(l => l.AddFilter((cat, lev) => 
                { 
                    // Filter information logs from Sample.Program
                    if (cat.StartsWith("Sample.Program"))
                    {
                        return lev >= LogLevel.Warning;
                    }
                    // else informational only
                    return lev >= LogLevel.Information;
                })
                .AddConsole()
                );

then

    logger.LogInformation("Information");
    logger.LogWarning("Warning");

displays only the warning message:

warn: Sample.Program[0]
      Warning

If I add .AddSerilog() like this:

services.AddLogging(l => l.AddFilter((cat, lev) => 
                { 
                    // Filter out all EF logs to stop it being so chatty
                    if (cat.StartsWith("Sample.Program"))
                    {
                        return lev >= LogLevel.Warning;
                    }
                    // else informational only
                    return lev >= LogLevel.Information;
                })
                .AddConsole()
                .AddSerilog()
                );

I see both messages from the serilog logger and only the warning message from the .net logger:

[08:47:47 INF] Information
[08:47:49 WRN] Warning
warn: Sample.Program[0]
      Warning

this seems to be due to this this line If I comment that line out then filtering works as expected. Was this left for debugging purposes? Should it be controlled via a parameter to the method? It doesn't seem like it is needed since you can control filtering outside.

@nblumhardt
Copy link
Member

Hi @dpoole73 - thanks for the note.

You can read some of the discussion regarding this in #98.

The eventual result of working through this was Serilog.AspNetCore's UseSerilog() and the related instructions in that README; see alternatively Serilog.Extensions.Hosting if you're not in a web app.

If your app's a plain console app, then configuring everything via Serilog (sinks, levels, filters, etc.) and just using Microsoft.Extensions.Logging as a logging abstraction will be a better experience. Let us know if you need more details on this.

Cheers,
Nick

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