question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request logging path based filtering

See original GitHub issue

Hi @nblumhardt just wanted to suggest a feature for the request logging middleware…

We’ve been using an earlier version of this middleware that I think you blogged about at some point. Anyway, one thing that we did is made it possible to filter out certain paths for example the health check endpoints. We send our logs to Sumologic and the bills can get expensive so we reduced logging by getting rid of these logs. We just change the log level to Debug.

For example:

var path = GetPath(httpContext);
switch (path)
{
    case "/":
    case "/ping":
    case "/live":
    case "/ready":
        level = LogEventLevel.Debug;
        break;
}

Would you be interested in this idea? An optional callback function in the RequestLoggingOptions which could decide the log level or to not log at all. The other way it could be done is by extending RequestLoggingMiddleware with a subclass perhaps.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
nblumhardtcommented, Aug 28, 2019

Just a note, this didn’t make it into 3.0, but would be good to consider further down the track.

In the meantime, Filter.ByExcluding() is a reasonable workaround in many cases. For example, using Serilog.Filters.Expressions, your health check endpoints could be excluded with:

    .Filter.ByExcluding("RequestPath not in ['/', '/ping', '/live', '/ready']")
1reaction
nblumhardtcommented, Oct 12, 2019

3.1.0 includes a new configuration option that works for this, although it’s a little bit subtle:

LogEventLevel GetLevel(HttpContext ctx, double elapsedMS, Exception ex)
{
    var path = GetPath(httpContext);
    switch (path)
    {
        case "/":
        case "/ping":
        case "/live":
        case "/ready":
            return LogEventLevel.Debug;
    }

    return ex != null || ctx.Response.StatusCode > 499 :
        LogEventLevel.Error : 
        LogEventLevel.Information;
}

app.UseSerilogRequestLogging(opts => opts.GetLevel = GetLevel);

#140 is queued up to release this, but there’s already a -dev build on NuGet to try. If it works for you, I think in the spirit of minimalism we should call this “done”. Let me know how you go!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Logging for IIS - Log Filtering
This section describes how to create a filter for a log definition that captures requests based on multiple criteria. The example in this ......
Read more >
Spring - Log Incoming Requests
In this quick tutorial, we'll learn the basics of logging incoming requests using Spring Boot's logging filter.
Read more >
Spring Boot - How to log all requests and responses with ...
In order to log all the requests with input parameters and body, we can use filters and interceptors. But while using a filter...
Read more >
java - Filter for logging http request parameters
I have written a filter that intercepts http calls and logs request parameters. public void doFilter(ServletRequest request, ServletResponse ...
Read more >
A Guide to Log Filtering: Tips for IT Pros
In this post, I'll show how you can filter log messages and make them useful for solving your application and infrastructure problems.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found