Flush doesn't block until all asynchronous logs have been written

490 views
Skip to first unread message

Charles M

unread,
Jan 26, 2015, 12:25:15 PM1/26/15
to nlog-...@googlegroups.com
I am using NLog to log to a RabbitMQ message queue and have written my own Log Target to achieve this.

At the end of my process I call LogManager.Shutdown() to free up all unmanaged resources.

To test the performance of my logger I have implement a loop over 1000 items and each is published to my message queue. I am using asynchronous logging.

The first problem I had is that when my application gets to the end of its processing and calls LogManager.ShutDown, all threads were teminated and I found I was loosing a lot of messages before they got written to my message queue.

I then modified my code to look like this....

// Processing
LogManager.Flush()
LogManager.Shutdown() 

 The problem I have now is that Flush() doesn't seem to block the main thread until all of the asynchronous messages have been written.


I have tried adding a time out to flush of say 1 minute which should be enough for my application to finish processing. However, I noticed that once all of the logs had been written, my application still waited for the full 1 minute before shutting down.

I also tried adding my own AsyncContinuation  but I am not really sure what this should look like or if it is the right approach to this problem.

1. Is this the expected behaviour that Flush() should block the thread ?

2. Is there a way to make sure that Flush() cleans out all of the messages from my log targets before my application shuts down the logger?

3. Can I do something with the continuation to achieve this?

I would appreciate some help as I need to have asynchronous logging without loosing messages.

Thanks in advance.

Kim Christensen

unread,
Jan 28, 2015, 3:21:30 PM1/28/15
to nlog-...@googlegroups.com

Flush doesn't block the thread when using async targets, therefore there is no way to tell when everything has been flushed. LogManager.Flush just calls flush on all targets which means it is up to the target to determine if flushing should block the thread.

Charles M

unread,
Jan 29, 2015, 5:10:51 AM1/29/15
to nlog-...@googlegroups.com
Hi, how can I make my target block the thread in this case?
Thanks.
Reply all
Reply to author
Forward
0 new messages