Search code examples
c#loggingnlog

What is the difference betwen AsyncWrapper and BufferingWrapper in NLog v2?


I was looking over some of the best practices for NLog when I noticed following target configuration:

<targets async="true">
  <default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
  <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
  <!-- other stuff -->
</targets>

From what I understand this wraps the file target with AsyncWrapper as well as with BufferingWrapper...

What is the difference between the two? Do I need both, since NLog site describes both as "buffering"....


Solution

  • Once there are enough messages (specified by bufferSize parameter) in the buffer, BufferingWrapper will block and write the messages to its target. The caller will need to wait until the writing is finished.

    AsynWrapper uses a separate thread to handle the writes. The calls return immediately and the caller can continue its work and the log gets written later.