4
votes

In my nlog configuration, I've set

<targets async="true">

with the understanding that all logging now happens asynchronously to my application workflow. (and I have noticed a performance improvement, especially on the Email target). This has me thinking about log sequence though. I understand that with async, one has no guarantee of the order in which the OS will execute the async work. So if, in my web app, multiple requests come in to the same method, each logging their occurrence to NLog, does this really mean that the sequence in which the events appear in my log target will not necessarily be the sequence in which the log method was called by the various requests?

If so, is this just a consequence of async that one has to live with? Or is there something I can do to keep have my logs reflect the correct sequence?

3

3 Answers

3
votes

Unfortunately this is something you have to live with. If it is important to maintain the sequence you'll have to run it synchronously.

But if it is possible for you to manually maintain a sequence number in the log message, it could be a solution.

0
votes

I know this is old and I'm just ramping up on NLog but if you see a performance increase for the email client, you may want to just assert ASYNC for the email target?

0
votes

NLog will not perform reordering of LogEvent sequence, by activating <targets async="true">. It just activates an internal queue, that provides better handling of bursts and enables batch-writing.

If a single thread writes 1000 LogEvents then they will NOT become out-of-order, because of async-handling.

If having 10 threads each writing 1000 LogEvents, then their logging will mix together. But the LogEvents of an individual thread will be in the CORRECT order.

But be aware that <targets async="true"> use the overflowAction=Discard as default. See also: https://github.com/nlog/NLog/wiki/AsyncWrapper-target#async-attribute-will-discard-by-default

For more details about performance. See also: https://github.com/NLog/NLog/wiki/performance