PostFilteringTargetWrapper Class |
Namespace: NLog.Targets.Wrappers
The PostFilteringTargetWrapper type exposes the following members.
Name | Description | |
---|---|---|
PostFilteringTargetWrapper |
Initializes a new instance of the PostFilteringTargetWrapper class.
| |
PostFilteringTargetWrapper(Target) |
Initializes a new instance of the PostFilteringTargetWrapper class.
| |
PostFilteringTargetWrapper(String, Target) |
Initializes a new instance of the PostFilteringTargetWrapper class.
|
Name | Description | |
---|---|---|
DefaultFilter |
Gets or sets the default filter to be applied when no specific rule matches.
| |
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.) | |
Rules |
Gets the collection of filtering rules. The rules are processed top-down
and the first rule that matches determines the filtering condition to
be applied to log events.
| |
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
(Inherited from Target.) | |
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
(Inherited from Target.) | |
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.
(Overrides TargetWrite(AsyncLogEventInfo).) | |
Write(IListAsyncLogEventInfo) |
Evaluates all filtering rules to find the first one that matches.
The matching rule determines the filtering condition to be applied
to all items in a buffer. If no condition matches, default filter
is applied to the array of log events.
(Overrides TargetWrite(IListAsyncLogEventInfo).) | |
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) |
Writes a log event 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.) | |
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.) |
This example works like this. If there are no Warn,Error or Fatal messages in the buffer only Info messages are written to the file, but if there are any warnings or errors, the output includes detailed trace (levels >= Debug).
To set up the target in the configuration file, use the following syntax:
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="file" xsi:type="BufferingWrapper" bufferSize="100"> 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 13 <rules> 14 <logger name="*" minlevel="Debug" writeTo="file" /> 15 </rules> 16</nlog>
To set up the log target programmatically use code like this:
1using System; 2 3using NLog; 4using NLog.Targets; 5using NLog.Targets.Wrappers; 6using System.Diagnostics; 7 8class Example 9{ 10 static void Main(string[] args) 11 { 12 FileTarget wrappedTarget = new FileTarget(); 13 wrappedTarget.FileName = "${basedir}/file.txt"; 14 15 PostFilteringTargetWrapper postFilteringTarget = new PostFilteringTargetWrapper(); 16 postFilteringTarget.WrappedTarget = wrappedTarget; 17 18 // set up default filter 19 postFilteringTarget.DefaultFilter = "level >= LogLevel.Info"; 20 21 FilteringRule rule; 22 23 // if there are any warnings in the buffer 24 // dump the messages whose level is Debug or higher 25 26 rule = new FilteringRule(); 27 rule.Exists = "level >= LogLevel.Warn"; 28 rule.Filter = "level >= LogLevel.Debug"; 29 30 postFilteringTarget.Rules.Add(rule); 31 32 BufferingTargetWrapper target = new BufferingTargetWrapper(); 33 target.BufferSize = 100; 34 target.WrappedTarget = postFilteringTarget; 35 36 NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 37 38 Logger logger = LogManager.GetLogger("Example"); 39 logger.Debug("log message"); 40 } 41}