HTTP Request Logging (original) (raw)
⚠️ HTTP Logging can potentially log personally identifiable information (PII). Consider the risk and avoid logging sensitive information.
Contents
- ASP.NET Core - UseHttpLogging introduced with .NET6
- ASP.NET Core - UseW3CLogging introduced with .NET6
- ASP.NET Core - Request Logging Middleware introduced with NLog v5
- ASP.NET Classic - Request Logging HttpModule introduced with NLog v5
- ASP.NET Core - Response Body Logging Middleware
UseHttpLogging introduced with NET6
.NET 6 includes a middleware implementation that captures HTTP request context as LogEvent Properties.
Use the LoggingFields-property to control how much data to capture in the logging. Notice RequestBody and ResponseBody has performance implications, as it requires buffering the entire body.
The logger category-name become Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseHttpLogging();
}
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; }); }
This can be combined with NLog JsonLayout to output the HTTP-request/response in structured format:
Notice the Microsoft HttpLoggingMiddleware outputs both a request-LogEvent and response-LogEvent for each HttpRequest. This can be changed to a single logevent by setting the option CombineLogs = true.
Notice the Microsoft HttpLoggingMiddleware will react to all requests (including .css + .js). These can be filtered away by using IHttpLoggingInterceptor
See also: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0
UseW3CLogging introduced with NET6
.NET 6 includes a middleware implementation that captures HTTP request context and writes to LogFile in W3C Extended Log Format.
Use the LoggingFields-property to control how much data to capture in the logging.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseW3CLogging();
}
public void ConfigureServices(IServiceCollection services) { services.AddW3CLogging(logging => { // Log all W3C fields logging.LoggingFields = W3CLoggingFields.All; logging.FileSizeLimit = 5 * 1024 * 1024; logging.RetainedFileCountLimit = 2; logging.FileName = "MyLogFile"; logging.LogDirectory = @"C:\logs"; logging.FlushInterval = TimeSpan.FromSeconds(2); }); }
See also: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/w3c-logger/?view=aspnetcore-6.0
Request Logging Middleware introduced with NLog5
Install NLog.Web.AspNetCore nuget-package and activate the middleware like this (Startup.cs):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseMiddleware<NLog.Web.NLogRequestLoggingMiddleware>(); }
Configure W3C Log-File with W3CExtendedLogLayout like this in NLog.config:
Configure JSON Log-File with JsonLayout like this in NLog.config:
Note to include the HTTP Request Duration, then one can use ${aspnet-request-duration} that outputs total milliseconds. See also NLog.Web.AspNetCore LayoutRenderers
Note to include the HTTP Posted Body, then one can use ${aspnet-request-posted-body} together with NLogRequestPostedBodyMiddleware.
Request Logging HttpModule introduced with NLog5
Install NLog.Web nuget-package for classic ASP.NET and activate like this (Global.asax)
public class MyGlobalApplication : System.Web.HttpApplication { public static IHttpModule NLogRequestLogging = new NLog.Web.NLogRequestLoggingModule();
public override void Init()
{
base.Init();
NLogRequestLogging.Init(this);
}}
Alternative can implement HTTP-request-logging using Action Filters.
Configure W3C Log-File with W3CExtendedLogLayout like this in NLog.config:
Configure JSON Log-File with JsonLayout like this in NLog.config:
Note to include the HTTP Request Duration, then one can use ${aspnet-request-duration} that outputs total milliseconds. See also NLog.Web LayoutRenderers
Note to include the HTTP Posted Body, then one can use ${aspnet-request-posted-body} together with NLogRequestPostedBodyModule.