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

Request logging path based filtering #100

Closed
jakejscott opened this issue Jul 15, 2019 · 6 comments
Closed

Request logging path based filtering #100

jakejscott opened this issue Jul 15, 2019 · 6 comments

Comments

@jakejscott
Copy link

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.

@nblumhardt
Copy link
Member

Thanks for the feedback, Jake 👍

I've been anticipating needing to add some functionality along these lines, need to give it some more thought, though. Will try to loop back to this before we RTM the 3.0 version.

@nblumhardt
Copy link
Member

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']")

@sungam3r
Copy link
Contributor

sungam3r commented Sep 5, 2019

@jakejscott In our projects (we use custom middleware for request logging), we did just what you said, a variety of delegates for filters and other settings in the IOptions<SomeMiddlewareOptions> that SomeMiddleware depends on.

@nblumhardt
Copy link
Member

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!

@sungam3r
Copy link
Contributor

Yes, I saw it. Over time the options tend to fill up with new settings, as numerous consumers always have different behavior requirements.

@nblumhardt
Copy link
Member

@sungam3r yes, there's always a delicate balance - this one made the bar because of (IIRC) four requests for it within the first month of shipping the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants