8

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"....

Community
  • 1
  • 1
zam6ak
  • 7,229
  • 11
  • 46
  • 84

2 Answers2

5

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.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43
  • 1
    BufferingWrapper appears (now) to be async if `slidingTimeout` is used. https://github.com/nlog/nlog/wiki/BufferingWrapper-target – Alex Angas Jul 26 '16 at 23:49
  • Only if the buffer is not filled before the flush timout – Inbar Barkai Sep 04 '19 at 08:58
  • @Juha << 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. >> I assume this means that the BufferingWrapper is safe to use in, for example, Azure Functions, without the risk of the function going out of scope *before* the buffered messages are written to the target, is that correct? – Matthew Aug 31 '20 at 17:24
2

BufferingWrapper works as a throttle, so it queues up messages before writing them to the actual target. For example, writing an email with all alerts every 1 minute.

AsyncWrapper uses a background thread for writing to the actual target, so the application-code logging will not be blocked. The AsyncWrapper improves concurrency when having many application-threads logging to the same target. The AsyncWrapper also improves target-throughput by writing in small batches, instead of one LogEvent at a time.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70