AspNetBufferingTargetWrapper Class |
Namespace: NLog.Web.Targets.Wrappers
The AspNetBufferingTargetWrapper type exposes the following members.
Name | Description | |
---|---|---|
AspNetBufferingTargetWrapper |
Initializes a new instance of the AspNetBufferingTargetWrapper class.
| |
AspNetBufferingTargetWrapper(Target) |
Initializes a new instance of the AspNetBufferingTargetWrapper class.
| |
AspNetBufferingTargetWrapper(Target, Int32) |
Initializes a new instance of the AspNetBufferingTargetWrapper class.
|
Name | Description | |
---|---|---|
BufferGrowLimit |
Gets or sets the maximum number of log events that the buffer can keep.
| |
BufferSize |
Gets or sets the number of log events to be buffered.
| |
GrowBufferAsNeeded |
Gets or sets a value indicating whether buffer should grow as needed.
| |
IsInitialized |
Gets a value indicating whether the target has been initialized.
(Inherited from Target.) | |
LoggingConfiguration |
Gets the logging configuration this target is part of.
(Inherited from Target.) | |
Name |
Gets or sets the name of the target.
(Inherited from Target.) | |
SyncRoot |
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.) | |
WrappedTarget |
Gets or sets the target that is wrapped by this target.
(Inherited from WrapperTargetBase.) |
Name | Description | |
---|---|---|
CloseTarget |
Closes the target to release any initialized resources
(Overrides TargetCloseTarget.) | |
Dispose |
Closes the target.
(Inherited from Target.) | |
Dispose(Boolean) |
Releases unmanaged and - optionally - managed resources.
(Inherited from Target.) | |
Flush |
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.) | |
FlushAsync |
Flush any pending log messages
(Inherited from WrapperTargetBase.) | |
InitializeTarget |
Initializes the target before writing starts
(Overrides TargetInitializeTarget.) | |
PrecalculateVolatileLayouts |
Calls the Precalculate(LogEventInfo) on each volatile layout
used by this target.
This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.) | |
RenderLogEvent(Layout, LogEventInfo) |
Renders the logevent into a string-result using the provided layout
(Inherited from Target.) | |
RenderLogEventT(LayoutT, LogEventInfo, T) |
Renders the logevent into a result-value by using the provided layout
(Inherited from Target.) | |
ResolveServiceT |
Resolve from DI ServiceRepository (Inherited from Target.) | |
ToString | Returns a string that represents the current object. (Inherited from WrapperTargetBase.) | |
Write(AsyncLogEventInfo) |
Writes async log event to the log target.
(Inherited from Target.) | |
Write(IListAsyncLogEventInfo) |
Writes an array of logging events to the log target. By default it iterates on all
events and passes them to "Write" method. Inheriting classes can use this method to
optimize batch writes.
(Inherited from Target.) | |
Write(LogEventInfo) |
Writes logging event to the log target. Must be overridden in inheriting
classes.
(Inherited from WrapperTargetBase.) | |
WriteAsyncLogEvent |
Writes the log to the target.
(Inherited from Target.) | |
WriteAsyncLogEvents(AsyncLogEventInfo) |
Writes the array of log events.
(Inherited from Target.) | |
WriteAsyncLogEvents(IListAsyncLogEventInfo) |
Writes the array of log events.
(Inherited from Target.) | |
WriteAsyncThreadSafe(AsyncLogEventInfo) |
Adds the specified log event to the buffer.
(Overrides TargetWriteAsyncThreadSafe(AsyncLogEventInfo).) | |
WriteAsyncThreadSafe(IListAsyncLogEventInfo) |
Writes an array of logging events to the log target, in a thread safe manner.
Any override of this method has to provide their own synchronization mechanism.
!WARNING! Custom targets should only override this method if able to provide their
own synchronization mechanism. Layout-objects are not guaranteed to be
thread-safe, so using them without a SyncRoot-object can be dangerous.
(Inherited from Target.) | |
WriteFailedNotInitialized |
LogEvent is written to target, but target failed to successfully initialize
(Inherited from Target.) |
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, for classic ASP.NET you need to add an entry in the httpModules section of web.config:
1<?xml version="1.0" ?> 2 <configuration> 3 <system.web> 4 <httpModules> 5 <add name="NLog" type="NLog.Web.NLogHttpModule, NLog.Web"/> 6 </httpModules> 7 </system.web> 8 </configuration>
1app.UseMiddleware<NLogBufferingTargetWrapperMiddleware>();
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).
1<?xml version="1.0" ?> 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4 <targets> 5 <target name="t" xsi:type="ASPNetBufferingWrapper"> 6 <target xsi:type="PostFilteringWrapper" defaultFilter="level >= LogLevel.Info"> 7 <target xsi:type="File" fileName="${basedir}/file.txt"/> 8 <when exists="level >= LogLevel.Warn" filter="level >= LogLevel.Debug"/> 9 </target> 10 </target> 11 </targets> 12 <rules> 13 <logger name="*" minlevel="Debug" writeTo="t" /> 14 </rules> 15</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:
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.SessionState; 8using NLog.Targets.Wrappers; 9using NLog.Targets; 10using NLog.Config; 11using NLog; 12 13namespace ASPNetBufferingWrapper 14{ 15 public class Global : System.Web.HttpApplication 16 { 17 protected void Application_Start(object sender, EventArgs e) 18 { 19 FileTarget fileTarget = new FileTarget(); 20 fileTarget.FileName = "${basedir}/logfile.txt"; 21 22 PostFilteringTargetWrapper postfilteringTarget = new PostFilteringTargetWrapper(); 23 ASPNetBufferingTargetWrapper aspnetBufferingTarget = new ASPNetBufferingTargetWrapper(); 24 aspnetBufferingTarget.WrappedTarget = postfilteringTarget; 25 postfilteringTarget.WrappedTarget = fileTarget; 26 27 postfilteringTarget.DefaultFilter = "level >= LogLevel.Info"; 28 FilteringRule rule = new FilteringRule(); 29 rule.Exists = "level >= LogLevel.Warn"; 30 rule.Filter = "level >= LogLevel.Debug"; 31 postfilteringTarget.Rules.Add(rule); 32 33 SimpleConfigurator.ConfigureForTargetLogging(aspnetBufferingTarget, LogLevel.Debug); 34 } 35 36 protected void Application_End(object sender, EventArgs e) 37 { 38 } 39 } 40}
Fully working C# project can be found in the Examples/Targets/Configuration API/ASPNetBufferingWrapper directory along with usage instructions.