-
Notifications
You must be signed in to change notification settings - Fork 165
HTTP Request Logging
- ASP.NET Core - UseHttpLogging introduced with .NET6
- ASP.NET Core - UseW3CLogging introduced with .NET6
- ASP.NET Core - Request Logging Middleware introduced with NLog5
- ASP.NET Classic - Request Logging HttpModule introduced with NLog5
.NET 6 includes a middleware implementation that captures HTTP request context as LogEvent Properties.
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;
});
}
See also: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0
.NET 6 includes a middleware implementation that captures HTTP request context and writes to LogFile in W3C Extended Log Format.
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
Install NLog.Web.AspNetCore nuget-package and activate the middleware like this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<NLog.Web.NLogRequestLoggingMiddleware>();
}
Configure W3C Log-File with W3CExtendedLogLayout like this in NLog.config:
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target type="file" name="w3c-file" filename="HttpRequests-W3C.log">
<layout type="W3CExtendedLogLayout"/>
</target>
</targets>
<rules>
<logger name="NLogRequestLogging" minLevel="Debug" writeTo="w3c-file" final="true" />
</rules>
</nlog>
Configure JSON Log-File with JsonLayout like this in NLog.config:
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target type="file" name="http-file" filename="HttpRequests-JSON.log">
<layout xsi:type="JsonLayout" includeAllProperties="true" maxRecursionLimit="1">
<attribute name="loglevel" layout="${level}" />
<attribute name="timestamp" layout="${date:format=o}" />
<attribute name="url" layout="${aspnet-request-url}" />
</layout>
</target>
</targets>
<rules>
<logger name="NLogRequestLogging" minLevel="Debug" writeTo="http-file" final="true" />
</rules>
</nlog>
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:
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target type="file" name="w3c-file" filename="HttpRequests-W3C.log">
<layout type="W3CExtendedLogLayout"/>
</target>
</targets>
<rules>
<logger name="NLogRequestLogging" minLevel="Debug" writeTo="w3c-file" final="true" />
</rules>
</nlog>
Configure JSON Log-File with JsonLayout like this in NLog.config:
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target type="file" name="http-file" filename="HttpRequests-JSON.log">
<layout xsi:type="JsonLayout" includeAllProperties="true" maxRecursionLimit="1">
<attribute name="loglevel" layout="${level}" />
<attribute name="timestamp" layout="${date:format=o}" />
<attribute name="url" layout="${aspnet-request-url}" />
</layout>
</target>
</targets>
<rules>
<logger name="NLogRequestLogging" minLevel="Debug" writeTo="http-file" final="true" />
</rules>
</nlog>
For general docs, check https://github.com/NLog/NLog/wiki