PostFilteringWrapper Target A target wrapper that filters buffered log entries based on a set of conditions that are evaluated on all events. | 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.PostFilteringTargetWrapper |  |  |  |  |  |  |  |  |  |
Remarks: PostFilteringWrapper must be used with some type of buffering target or wrapper, such as AsyncTargetWrapper, BufferingWrapper or ASPNetBufferingWrapper. Parameters (blue fields are required):| Name | Type | Description |
|---|
| name | string | | | defaultFilter | string | | Default filter to be applied when no specific rule matches. |
| | wrappedTarget | Target | | The target that this target wraps. |
| | Rules |
Collection of
FilteringRule. Each element is represented as <when/>
| | Name | Type | Description |
|---|
| exists | string
[c()] | | | filter | string
[c()] | | Resulting filter to be applied when the condition matches. |
|
|
Example: 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). You can plug in a different type of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different functionality. To set up the target in the configuration file, use the following syntax: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="BufferingWrapper" bufferSize="100">
<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="file"/>
</rules>
</nlog>
The above examples assume just one target and a single rule. See below for a programmatic configuration that's equivalent to the above config file: using System;
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Diagnostics;
class Example
{
static void Main(string[] args)
{
FileTarget wrappedTarget = new FileTarget();
wrappedTarget.FileName = "${basedir}/file.txt";
PostFilteringTargetWrapper postFilteringTarget = new PostFilteringTargetWrapper();
postFilteringTarget.WrappedTarget = wrappedTarget;
// set up default filter
postFilteringTarget.DefaultFilter = "level >= LogLevel.Info";
FilteringRule rule;
// if there are any warnings in the buffer
// dump the messages whose level is Debug or higher
rule = new FilteringRule();
rule.Exists = "level >= LogLevel.Warn";
rule.Filter = "level >= LogLevel.Debug";
postFilteringTarget.Rules.Add(rule);
BufferingTargetWrapper target = new BufferingTargetWrapper();
target.BufferSize = 100;
target.WrappedTarget = postFilteringTarget;
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
}
}
Back to the target list. |