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.

HttpLoggingMiddleware could allow custom code to decide whether to log or not

See original GitHub issue

Background and Motivation

The HttpLoggingMiddleware added recently works based on configuration options in HttpLoggingOptions.

The currently implementation is basically all or nothing. You either add the middleware or not. Even if you can decide what is redacted or not and if request/response bodies are included.

It would be nice if HttpLoggingOptions included a delegate to let the application (custom code) decide whether to log or not for a specific request.

Proposed API

namespace Microsoft.AspNetCore.HttpLogging
{
    public sealed class HttpLoggingOptions
    {
+       public Func<HttpContext, bool> Selector { get; set; } = context => true;
    }
}

Usage Examples

  • Log only failed requests.
  • Log request body only for specific endpoints.

Alternative Designs

You can implement your own middleware but that beats the whole purpose of implementing this feature in the framework. An alternative would be to make HttpLoggingMiddleware public and add some kind of extensibility to it. At least one could implement that custom middleware but reuse the framework implementation (e.g. request buffering).

Risks

The risk is that performance of HTTP logging would depend on the custom code implemented in the delegate.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
Tratchercommented, Feb 24, 2023

Rather than on/off, you’d want something more granular like HttpLoggingFields (and return None for off).

namespace Microsoft.AspNetCore.HttpLogging
{
    public sealed class HttpLoggingOptions
    {
+       public Func<HttpContext, HttpLoggingFields>? FieldSelector { get; set; } 
    }
}

This is similar to the proposed endpoint attribute in https://github.com/dotnet/aspnetcore/issues/43222.

2reactions
hugoqribeirocommented, Dec 27, 2021

UseWhen would problably work better for the “log only for specific endpoints”. It would not help for the “log only failed requests” right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use HTTP logging in ASP.NET Core 6
The HTTP logging middleware included in ASP.NET Core allows you to log request and response data including headers, body, and common properties.
Read more >
Smart Logging Middleware for ASP.NET Core - Structured Blog
We've seen how a simple customized logging strategy can not only produce cleaner events, but also reduce the volume of infrastructure log events ......
Read more >
Implement a custom logging provider - .NET
In this article, you'll learn how to implement a custom logging provider that can be used to colorize logs in the console.
Read more >
How to turn off the logging done by the ASP.NET core ...
Logging ; public void ConfigureServices(IServiceCollection services) ... implementation to decide whether it wants to log that source or not.
Read more >
Don't let ASP.NET Core Console Logging Slow your App down
Using the Logger in your Code​​ The T in ILogger<T> provides a logging context that is shown on log messages so you can...
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