HTTP Logging in ASP.NET Core

HTTP Logging is a middleware that logs information about HTTP requests and HTTP responses. HTTP logging provides logs of:

  • HTTP request information
  • Common properties
  • Headers
  • Body
  • HTTP response information

HTTP Logging is valuable in several scenarios to:

  • Record information about incoming requests and responses.
  • Filter which parts of the request and response are logged.
  • Filtering which headers to log.

HTTP Logging can reduce the performance of an app, especially when logging the request and response bodies. Consider the performance impact when selecting fields to log. Test the performance impact of the selected logging properties.

Warning

HTTP Logging can potentially log personally identifiable information (PII). Consider the risk and avoid logging sensitive information.

Enabling HTTP logging

HTTP Logging is enabled with UseHttpLogging, which adds HTTP logging middleware.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpLogging();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGet("/", async context =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    });
}

By default, HTTP Logging logs common properties such as path, status-code, and headers for requests and responses. The output is logged as a single message at LogLevel.Information.

Sample request output

HTTP Logging options

To configure the HTTP logging middleware, call AddHttpLogging in ConfigureServices.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}

LoggingFields

HttpLoggingOptions.LoggingFields is an enum flag that configures specific parts of the request and response to log. LoggingFields defaults to RequestPropertiesAndHeaders | ResponsePropertiesAndHeaders.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}
Flag Flag for logging the HTTP Value
None No logging. 0x0
RequestPath Request Path, which includes both the Path and PathBase. 0x1
RequestQuery Request QueryString. 0x2
RequestProtocol Request Protocol. 0x4
RequestMethod Request Method. 0x8
RequestScheme Request Scheme. 0x10
ResponseStatusCode Response StatusCode. 0x20
RequestHeaders Request Headers. Request headers are logged as soon as the middleware is invoked. Headers are redacted by default with the character '[Redacted]' unless specified in the HttpLoggingOptions.RequestHeaders. 0x40
ResponseHeaders Response Headers. Response headers are logged when the Body is written to or when StartAsync is called. Headers are redacted by default with the character '[Redacted]' unless specified in the HttpLoggingOptions.ResponseHeaders. 0x80
RequestTrailers Request IHttpRequestTrailersFeature.Trailers. Request Trailers are currently not logged. 0x100
ResponseTrailers Response IHttpResponseTrailersFeature.Trailers. Response Trailers are currently not logged. 0x200
RequestBody Request Body. Logging the request body has performance implications, as it requires buffering the entire request body up to HttpLoggingOptions.RequestBodyLogLimit. 0x400
ResponseBody Response Body. Logging the response body has performance implications, as it requires buffering the entire response body up to HttpLoggingOptions.ResponseBodyLogLimit. 0x800
RequestProperties Flag for logging a collection of HTTP Request properties, including RequestPath, RequestQuery, RequestProtocol, RequestMethod, and RequestScheme. RequestPath | RequestQuery | RequestProtocol | RequestMethod | RequestScheme
RequestPropertiesAndHeaders Flag for logging HTTP Request properties and headers. Includes RequestProperties and RequestHeaders. RequestProperties | RequestHeaders
ResponsePropertiesAndHeaders Flag for logging HTTP Response properties and headers. Includes ResponseStatusCode and ResponseHeaders. ResponseStatusCode | ResponseHeaders
Request Flag for logging the entire HTTP Request. Includes RequestPropertiesAndHeaders and RequestBody. Logging the request body has performance implications, as it requires buffering the entire request body up to HttpLoggingOptions.RequestBodyLogLimit. RequestPropertiesAndHeaders | RequestBody
Response Flag for logging the entire HTTP Response. Includes ResponseStatusCode, ResponseHeaders, and ResponseBody. Logging the response body has performance implications, as it requires buffering the entire response body up to HttpLoggingOptions.ResponseBodyLogLimit. ResponseStatusCode | ResponseHeaders | ResponseBody
All Flag for logging both the HTTP Request and Response. Includes Request and Response. Logging the request and response body has performance implications, as it requires buffering the entire request and response body up to the HttpLoggingOptions.RequestBodyLogLimit and HttpLoggingOptions.ResponseBodyLogLimit. Request | Response

RequestHeaders

RequestHeaders are a set of HTTP Request Headers that are allowed to be logged. Header values are only logged for header names that are in this collection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}

ResponseHeaders

ResponseHeaders are a set of HTTP Response Headers that are allowed to be logged. Header values are only logged for header names that are in this collection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}

MediaTypeOptions

MediaTypeOptions provides configuration for selecting which encoding to use for a specific media type.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}

MediaTypeOptions methods

  • public void AddText(string contentType)
    • Adds a contentType to be used for logging as text using UTF-8 encoding.
  • public void AddText(string contentType, Encoding encoding)
    • Adds a contentType to be used for logging as text using the specified encoding.
  • public void AddBinary(MediaTypeHeaderValue mediaType)
    • Adds a MediaTypeHeaderValue to be used for logging as binary.
  • public void AddBinary(string contentType)
    • Adds a content to be used for logging as text using the specified content type.
  • public void Clear()
    • Clears all MediaTypes.

RequestBodyLogLimit

Maximum request body size to log, in bytes. Defaults to 32 KB.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}

ResponseBodyLogLimit

Maximum response body size to log, in bytes. Defaults to 32 KB.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpLogging(logging =>
    {
        // Customize HTTP logging here.
        logging.LoggingFields = HttpLoggingFields.All;
        logging.RequestHeaders.Add("My-Request-Header");
        logging.ResponseHeaders.Add("My-Response-Header");
        logging.MediaTypeOptions.AddText("application/javascript");
        logging.RequestBodyLogLimit = 4096;
        logging.ResponseBodyLogLimit = 4096;
    });
}