overview
HTTP logging middleware logs information about incoming HTTP requests and HTTP responses including common properties, headers, and the body.
enabling
- Call
AddHttpLoggingandUseHttpLogging:Program.csvar 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.jsonat 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
WithHttpLoggingextension 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.RequestHeadersandResponseHeaders— collections of header names to log. In the above example,sec-ch-uarequest 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-urlencodedormultipart/form-data. MediaTypeOptionsincludes 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.RequestBodyLogLimitandResponseBodyLogLimit— 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.