Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

logging

nlog

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

like image 588
zam6ak Avatar asked Mar 28 '12 19:03

zam6ak


2 Answers

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.

like image 113
Juha Palomäki Avatar answered Oct 20 '22 23:10

Juha Palomäki


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.

like image 34
Rolf Kristensen Avatar answered Oct 21 '22 00:10

Rolf Kristensen