8

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?

3 Answers 3

23

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, can you give an example of a Log.Info call that would get ignored because of this? I am not sure to understand
0

I had a very similar situation (don't want to be logging all Health queries), and found the solution on the official documentation itself: https://github.com/serilog/serilog-expressions

It's like your result, although yours didn't work for me; but their solution is entirely defined by a string so no idea how I would tweak it as a power user, but it worked just fine for my purposes.

The specific piece of code is this:

.Filter.ByExcluding("RequestPath like '%/health%'")

Comments

0

In case you are talking about UseSerilogRequestLogging there is an article from Andrew Lock: https://andrewlock.net/using-serilog-aspnetcore-in-asp-net-core-3-excluding-health-check-endpoints-from-serilog-request-logging/#customising-the-log-level-used-for-serilog-request-logs

My code sample is

app.UseSerilogRequestLogging(options =>
{
    // Log all requests but skip /health and /metrics requests
    options.GetLevel = (httpContext, _, ex) => ex == null
        ? httpContext.Request.Path.Value?.StartsWith("/health") == true || httpContext.Request.Path.Value?.StartsWith("/metrics") == true
            ? LogEventLevel.Debug
            : LogEventLevel.Information
        : LogEventLevel.Error;
    options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
    {
        if (httpContext.Request.Host.Value is not null)
            diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
        diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
    };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.