AspNetBufferingWrapper target
Buffers log events for the duration of ASP.NET request and sends them down to the wrapped target at the end of a request.
Platforms Supported: All
Introduced with NLog.Web.AspNetCore v5.2.1 and NLog.Web v4.0
<targets>
<target xsi:type="AspNetBufferingWrapper"
name="String"
bufferGrowLimit="Integer"
growBufferAsNeeded="Boolean"
bufferSize="Integer">
<target xsi:type="wrappedTargetType" ...target properties... />
</target>
</targets>
- name - Name of the target.
-
bufferSize - Initial capacity for the buffering of log events. Integer Default: 100
-
bufferGrowLimit - Maximum number of log events that the buffer can keep, when growing beyond the limit and overflows then it will drop the oldest logevents. Integer Default: 0 (Disable growing)
-
growBufferAsNeeded - Indicates whether buffer is allow to grow up to bufferGrowLimit. Boolean Default: True
Typically this target is used in cooperation with PostFilteringTargetWrapper to provide verbose logging for failing requests and normal or no logging for successful requests. We need to make the decision of the final filtering rule to apply after all logs for a page have been generated.
<targets>
<target xsi:type="AspNetBufferingWrapper" name="aspnetbuffer" bufferGrowLimit="1000">
<target xsi:type="PostFilteringWrapper" defaultFilter="level >= LogLevel.Info">
<target xsi:type="File" fileName="request-failed-${shortdate}.log" />
<when exists="level >= LogLevel.Warn" filter="level >= LogLevel.Debug"/>
</target>
</target>
</targets>
To use this target with NLog.Web.AspNetCore, then you need to enable the NLogBufferingTargetWrapperMiddleware
in the Startup.cs file
using NLog.Web;
app.UseMiddleware<NLogBufferingTargetWrapperMiddleware>();
where app is an IApplicationBuilder.
To use this target with NLog.Web, then you need to update web.config
to register NLogHttpModule
:
<?xml version="1.0" ?>
<configuration>
<system.web> <!-- httpModules for classic mode -->
<httpModules>
<add name="NLog" type="NLog.Web.NLogHttpModule, NLog.Web"/>
</httpModules>
</system.web>
<system.webServer> <!-- modules for integrated mode -->
<modules>
<add name="NLog" type="NLog.Web.NLogHttpModule, NLog.Web" />
</modules>
</system.webServer>
</configuration>
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json