Overview
HTTP logging middleware logs information about incoming HTTP requests and HTTP responses including common properties, headers, and the body.
Enabling
- Call
AddHttpLogging
andUseHttpLogging
:Program.cs
var builder = WebApplication.CreateBuilder(args); // the empty lambda here uses default logging options: builder.Services.AddHttpLogging(o => { }); var app = builder.Build(); app.UseHttpLogging(); if (!app.Environment.IsDevelopment()) app.UseExceptionHandler("/Error"); app.UseStaticFiles(); app.MapGet("/", () => "Hello World!"); app.Run();
- Update
appsettings.Development.json
at the"LogLevel"
statement:"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
Configuration
Order of Precedence
Logging configuration follows this order of precedence:
- Global configuration from
HttpLoggingOptions
(set by callingAddHttpLogging)
- Endpoint-specific configuration (via
WithHttpLogging
extension method (Minimal API apps) or[HttpLogging]
attribute (Controller-based apps)) IHttpLoggingInterceptor
Overview
Use the lambda in AddHttpLogging
to configure HttpLoggingOptions
:
// ...
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
logging.RequestHeaders.Add("sec-ch-ua");
logging.ResponseHeaders.Add("MyResponseHeader");
logging.MediaTypeOptions.AddText("application/javascript");
logging.RequestBodyLogLimit = 4096;
logging.ResponseBodyLogLimit = 4096;
logging.CombineLogs = true;
});
// ...
UseHttpLogging
before the call to UseStaticFiles
.
Details on HttpLoggingOptions
From the above example:
HttpLoggingOptions.LoggingFields
— an enum that configures specific parts of the request and response to logHttpLoggingOptions.RequestHeaders
andResponseHeaders
— collections of header names to log. In the above example,sec-ch-ua
request headers are logged.HttpLoggingOptions.MediaTypeOptions
— provides configuration for selecting which encoding to use for a specific media type.- This approach can also be used to log, for example, form data (which is not normally logged), but specifying a media type such as
application/x-www-form-urlencoded
ormultipart/form-data
. MediaTypeOptions
includes methodsAddText
,AddBinary
, andClear
.
- This approach can also be used to log, for example, form data (which is not normally logged), but specifying a media type such as
HttpLoggingOptions.RequestBodyLogLimit
andResponseBodyLogLimit
— set the limit, in bytes, to which to limit the logging of a request or response body.HttpLoggingOptions.CombineLogs
— boolean if all logs for a request and response should be consolidated into one log at the end.
Endpoint Logging Configuration
In Minimal API Apps
Use WithHttpLogging
extension method:
app.MapGet("/response", () => "Hello World! (logging response)")
.WithHttpLogging(HttpLoggingFields.ResponsePropertiesAndHeaders);
In Controller-based Apps
Use [HttpLogging]
attribute:
app.MapGet("/duration", [HttpLogging(loggingFields: HttpLoggingFields.Duration)]
() => "Hello World! (logging duration)");
IHttpLoggingInterceptor [ Documentation]
An interface for a service that can be implemented to handle per-request and per-response callbacks for customizing what details get logged.