ram dvash
ram dvash

Reputation: 190

NLog: Why BufferingWrapper does not seem to buffer log entries

I have a simple .Net core (2.2) Console application. I am trying to configure it to work with NLog. For simplicity, I am using "File" target. I am trying to configure Buffering Wrapper so I wrapped the target as follows:

<target name="bufferWrapper" xsi:type="BufferingWrapper" bufferSize="100">
   <target name="logfile" xsi:type="File" fileName="file.txt" />
</target>

When I stop in debug mode at a breakpoint immediately after writing to Nlog:

Logger.Info("Hello world");

I see that the log entry has already been added to "file.txt". I would have expected it flushed the entries to the file only after accumulating 100 log entries (as bufferSize=100).

Thanks for any help

Upvotes: 1

Views: 1093

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19877

When using a target-wrapper then one should write to the target-wrapper, instead of the actual target.

Something like this:

<nlog>
    <targets>
       <target name="bufferWrapper" xsi:type="BufferingWrapper" bufferSize="100">
          <target name="logfile" xsi:type="File" fileName="file.txt" />
       </target>
    </targets>
    <rules>
       <logger name="*" writeTo="bufferWrapper" /> <!-- Do not write directly to logfile -->
    </rules>
</nlog>

Btw. BufferingWrapper is a throttle-mechanism for delaying (or even discarding) logevents. If performance is important then one should use <targets async="true"> or AsyncWrapper directly.

Upvotes: 5

Related Questions