Skip to content

AutoFlushWrapper target

Rolf Kristensen edited this page Aug 23, 2020 · 14 revisions

Causes a flush on a wrapped target if LogEvent satisfies provided condition.
If condition isn't set, a flush will occur after each successful write.

Platforms Supported: All

Configuration Syntax

<targets>
  <target xsi:type="AutoFlushWrapper" name="String" condition="Condition">
    <target xsi:type="wrappedTargetType" ...target properties... />
  </target>
</targets>

Parameters

General Options

  • name - Name of the target.

  • condition - Condition is expression used to determine if flush must be executed.

    Introduced in NLog 4.4

  • asyncFlush - Delay the flush until the LogEvent has been confirmed as written. Default true.

    Introduced in NLog 4.4.6 where BufferingWrapper-target defaults to false, to avoid waiting long time (forever) to flush

    NLog 4.7 implements same behavior for AsyncTaskTarget so it defaults to false, to reduce waiting time for flush.

  • flushOnConditionOnly - Ignores explicit flush operations caused by calling NLog.LogManager.Flush() or NLog.LogManager.Shutdown(). Useful when combined with BufferingWrapper and overflowAction=Discard.

    Introduced in NLog 4.6.6

Examples

Flush into target on each write

<?xml version="1.0" ?>
<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="AutoFlushWrapper">
            <target xsi:type="File" fileName="${basedir}/file.txt" />
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

Flush into target, if LogEvent level >= Warn (introduced in NLog 4.4)

<?xml version="1.0" ?>
<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="AutoFlushWrapper" condition="level >= LogLevel.Warn">
            <target xsi:type="bufferingWrapper" overflowAction="Discard">
               <target xsi:type="File" fileName="${basedir}/file.txt" />
            </target>
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>
Clone this wiki locally