Skip to content

AspNetBufferingWrapper target

Rolf Kristensen edited this page Sep 23, 2023 · 19 revisions

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

Configuration Syntax

<targets>
  <target xsi:type="AspNetBufferingWrapper"
          name="String"
          bufferGrowLimit="Integer"
          growBufferAsNeeded="Boolean"
          bufferSize="Integer">
    <target xsi:type="wrappedTargetType" ...target properties... />
  </target>
</targets>

Parameters

General Options

  • name - Name of the target.

Buffering Options

  • 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

Remarks

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 &gt;= LogLevel.Info">
            <target xsi:type="File" fileName="request-failed-${shortdate}.log" />
            <when exists="level &gt;= LogLevel.Warn" filter="level &gt;= LogLevel.Debug"/>
        </target>
    </target>
</targets>

ASP.NET Core Setup

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.

ASP.NET Setup

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>
Clone this wiki locally