ASPNetBufferingWrapper Target A target that buffers log events for the duration of the ASP.NET Request and sends them down to the wrapped target as soon as the request ends. | Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix |
|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 |
|---|
| NLog.dll | NLog.Targets.Wrappers.ASPNetBufferingTargetWrapper |  |  |  | | |  |  |  |  |
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. To use this target, you need to add an entry in the httpModules section of web.config: <configuration>
<system.web>
<httpModules>
<add name="NLog" type="NLog.Web.NLogHttpModule, NLog"/>
</httpModules>
</system.web>
</configuration>
Parameters (blue fields are required):| Name | Type | Description |
|---|
| name | string | | | bufferGrowLimit | integer | | The maximum number of log events that the buffer can keep. |
| | bufferSize | integer | | The number of log events to be buffered. Default value is: 4000. |
| | growBufferAsNeeded | boolean | | Grow the buffer when it gets full. Default value is: False. true causes the buffer to expand until BufferGrowLimit is hit, false causes the buffer to never expand and lose the earliest entries in case of overflow. |
| | wrappedTarget | Target | | The target that this target wraps. |
|
Example:To set up the ASP.NET Buffering target wrapper configuration file, put the following in web.nlog file in your web application directory (this assumes that PostFilteringWrapper is used to provide the filtering and actual logs go to a file). <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="t" xsi:type="ASPNetBufferingWrapper">
<target xsi:type="PostFilteringWrapper"
defaultFilter="level >= LogLevel.Info">
<target xsi:type="File" fileName="${basedir}/file.txt"/>
<when exists="level >= LogLevel.Warn" filter="level >= LogLevel.Debug"/>
</target>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="t"/>
</rules>
</nlog>
This assumes just one target and a single rule. More configuration options are described here. To configure the target programmatically, put the following piece of code in your Application_OnStart() handler in Global.asax.cs or some other place that gets executed at the very beginning of your code: using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using NLog.Targets.Wrappers;
using NLog.Targets;
using NLog.Config;
using NLog;
namespace ASPNetBufferingWrapper
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
FileTarget fileTarget = new FileTarget();
fileTarget.FileName = "${basedir}/logfile.txt";
PostFilteringTargetWrapper postfilteringTarget = new PostFilteringTargetWrapper();
ASPNetBufferingTargetWrapper aspnetBufferingTarget = new ASPNetBufferingTargetWrapper();
aspnetBufferingTarget.WrappedTarget = postfilteringTarget;
postfilteringTarget.WrappedTarget = fileTarget;
postfilteringTarget.DefaultFilter = "level >= LogLevel.Info";
FilteringRule rule = new FilteringRule();
rule.Exists = "level >= LogLevel.Warn";
rule.Filter = "level >= LogLevel.Debug";
postfilteringTarget.Rules.Add(rule);
SimpleConfigurator.ConfigureForTargetLogging(aspnetBufferingTarget, LogLevel.Debug);
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Fully working C# project can be found in the Examples/Targets/Configuration API/ASPNetBufferingWrapper directory along with usage instructions. Back to the target list. |