N
N
NewDevLab2021-06-18 13:25:25
ASP.NET
NewDevLab, 2021-06-18 13:25:25

Filters in serilog?

Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
        .Enrich.FromLogContext()
        .Filter.ByIncludingOnly("@Level is null")
        .WriteTo.Console()
        .CreateLogger();

Why is level always null? those. all logs are output to the console. "@Level = 'Information'" outputs nothing.
A minimal example, a clean project from a VS template with Serilog.aspnetcore and Serilog.Expressions connected, the config is set from the code.

UPD:
.Filter.ByIncludingOnly(a => a.Level == LogEventLevel.Information)

that's how it works. then a question, and it to set declaratively in a config? the documentation is clearly not the same ...

UPD2:
i.e. everyone here works in production and does not ask the question of structured logs. cool.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NewDevLab, 2021-06-27
@NewDevLab

Connected Serilog.Expressions, and read docks from Serilog.Filter.Expressions.
muddler

D
Denis, 2021-06-24
@den_labs

You are asking to be sorted out for you in the current code. It's better to describe the problem you are trying to solve.
I didn't understand what you want from the question either.
I found this code in my stash, maybe it will help you, there is nothing in the config, everything is through the code:

Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File("info.txt", LogEventLevel.Information)
                //.WriteTo.File("debug.txt", LogEventLevel.Debug)
                .WriteTo.Console(LogEventLevel.Information, theme: SystemConsoleTheme.Colored)
                .CreateLogger();


        private static void ConfigureServices(IServiceCollection services, IExternalServerSettings serverSettings)
        {
            ......
            services.AddLogging(lb =>
            {
                lb.SetMinimumLevel(LogLevel.Debug);
                lb.AddFilter((type, level) => !type.Contains("System"));
            });
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question