Last updated: 2006-09-18

This website is based on NLog v1.0. Click here to view the documentation for other versions.

PostFilteringWrapper Target


A target wrapper that filters buffered log entries based on a set of conditions that are evaluated on all events.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.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):

NameTypeDescription
namestring
The name of the target.
defaultFilterstring
Default filter to be applied when no specific rule matches.
wrappedTargetTarget
The target that this target wraps.
Rules Collection of FilteringRule. Each element is represented as <when/>
NameTypeDescription
existsstring  [c()]
Condition to be tested.
filterstring  [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.
Webwww.nlog-project.org