Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude specific endpoint serilog logging using aspnet core

We have an aspnetcore application and we have a healthcheck that makes request to an endpoint every x seconds. Our API is using serilog, logging level information which log each request made to the API.

My question is: How can we filter to exclude from log requests made to a specific endpoint?

Just for example this is our logging code today:

        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                .Enrich.FromLogContext()                    
                .Filter.ByExcluding(c => c.MessageTemplate.Text.Contains("Request"))
                .WriteTo.Console()
                .WriteTo.RollingFile(hostingContext.Configuration["log-path"]))                    
            .Build();

Today we can only filter all requests. How do we filter a specific endpoint request?

like image 998
Rafael Miceli Avatar asked Dec 23 '22 05:12

Rafael Miceli


1 Answers

I found the way to do this.

Instead of Exclude by MessageTemplate, I excluded by Property Value.

The filter is like this:

Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))

In the end the code should look like this:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
        .Enrich.FromLogContext()                  
        .Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))
        .WriteTo.Console()
        .WriteTo.RollingFile(hostingContext.Configuration["log-path"]))                    
    .Build();

Hope this helps someone as helped me!

like image 104
Rafael Miceli Avatar answered May 09 '23 02:05

Rafael Miceli