Skip to content

Add RequestLoggingMiddleware #3700

@davidfowl

Description

@davidfowl

There's lots of advice on how to log the headers and body of an incoming and outgoing http request (and response) on the interwebs:

To name a few of them. They are not done in very safe ways:

  • Some buffer the entire request body of every request (not respecting the IHttpBufferingFeature). This results in memory bloats (a potential DoS)
  • Some don't rewind the Stream properly.
  • The implementations aren't lazy
  • There are security implications as well (sensitive data may be in headers, other PII, GDPR?)

It seems like lots of people are doing this and doing it incorrectly and inefficiently, we should add a middleware here as this is very common and requires no new dependencies.

Strawman:

public class RequestLoggingMiddlewareExtensions
{ 
      public IApplicationBuilder UseRequestLogging(this IApplicationBuilder app) { }
      public IApplicationBuilder UseRequestLogging(this IApplicationBuilder app, RequestLoggingOptions options) { }
}

public class RequestLoggingOptions
{
     // Determines if the cookie header is logged
     public bool LogCookieHeader { get; set; }
     // Determines if the authorization header is logged
     public bool LogAuthorizationHeader { get; set; }
     // Determines the verbosity of the logs
     public RequestLoggingLevel RequestLogLevel { get; set; }
}

[Flags]
public enum RequestLoggingLevel
{
    None = 0,
    RequestHeaders = 1,
    ResponseHeaders = 2,
    RequestBody = 4,
    ResponseBody = 8,
    AllRequest = RequestHeaders | RequestBody
    AllResponse = RequestHeaders | RequestBody
    All = AllRequest | AllResponse
}

cc @Tratcher

Metadata

Metadata

Assignees

Labels

affected-mostThis issue impacts most of the customersarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresenhancementThis issue represents an ask for new feature or an enhancement to an existing oneseverity-minorThis label is used by an internal tool

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions