FilteringTargetWrapper Class |
Namespace: NLog.Targets.Wrappers
The FilteringTargetWrapper type exposes the following members.
Name | Description | |
---|---|---|
FilteringTargetWrapper |
Initializes a new instance of the FilteringTargetWrapper class.
| |
FilteringTargetWrapper(Target, ConditionExpression) |
Initializes a new instance of the FilteringTargetWrapper class.
| |
FilteringTargetWrapper(String, Target, ConditionExpression) |
Initializes a new instance of the FilteringTargetWrapper class.
|
Name | Description | |
---|---|---|
Condition |
Gets or sets the condition expression. Log events who meet this condition will be forwarded
to the wrapped target.
| |
Filter |
Gets or sets the filter. Log events who evaluates to Ignore will be discarded
| |
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
(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) |
Checks the condition against the passed log event.
If the condition is met, the log event is forwarded to
the wrapped target.
(Overrides TargetWrite(AsyncLogEventInfo).) | |
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.
(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 causes the messages not contains the string '1' to be ignored.
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="FilteringWrapper" condition="contains('${message}','1')"> 6 <target xsi:type="File" fileName="${basedir}/file.txt" /> 7 </target> 8 </targets> 9 10 <rules> 11 <logger name="*" minlevel="Debug" writeTo="file" /> 12 </rules> 13</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 FilteringTargetWrapper filteringTarget = new FilteringTargetWrapper(); 16 filteringTarget.WrappedTarget = wrappedTarget; 17 18 filteringTarget.Condition = "contains('${message}','1')"; 19 20 NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(filteringTarget, LogLevel.Debug); 21 22 Logger logger = LogManager.GetLogger("Example"); 23 logger.Debug("log message 0"); 24 logger.Debug("log message 1"); 25 logger.Debug("log message 2"); 26 logger.Debug("log message 11"); 27 } 28}