Writing to a log4net FileAppender with multiple threads performance problems

风格不统一 提交于 2019-12-03 07:23:10

Log4net doesn't support the exact scenario you describe. It does however offer other appenders that don't lock, like the database appender, or the UDP appender. Here are a couple of ideas:

  1. Log your messages to a message queue, and then have a reader (or several readers) read messages off the queue and write them to a log. This provides a reliable mechanism to send/write messages. To be honest I don't know if there's already a MSMQ appender, but writing it yourself wouldn't be too hard.

  2. Use the UDP appender to send messages and then write your own service that listens to these messages and writes them to a file.

I think you can detect a theme here... basically use one of the non blocking appenders (or write your own) and implement your own service that receives the messages from the appenders and writes them to a file.

Check out The Object Guy's logger for a high performance, multi-thread safe logger with ability for async logging as well as many other features - quite nice I think - http://www.theobjectguy.com/DotNetLog/. See the Multi-threaded video on this page.

It is quite desirable to avoid adding any I/O to your targets threads, so sending a message to the logging collector thread is a good idea. I also sometimes use System::Diagnostics::Debug::WriteLine from the target thread to dump out its output, but there's bound to be a little locking inside there anyways.

Of course adding any extra logging is going to lead to "Heisenberg" effects, so you have to somehow know when these effects are negligible and when they're not, in order to have useful logging of your high performance threads.

If you want to get a little fancier, you can have each thread save its own message list, then dump it out somewhere after so many iterations. So the data would only useful in the period of time before any thread does its logging I/O, but maybe you could capture enough info for your debugging. Also, you get the fun task of collating the individual thread logs into a single log for analysis.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!