Running Log4Net appenders on separate thread

﹥>﹥吖頭↗ 提交于 2019-12-21 05:22:15

问题


Currently, I've got my own logging system, where the log is essentially a queue, with a separate thread listening to that queue and doing all of the actual write operations. The system processes a TON of log messages, files can easily exceed 10 MB sizes in minutes, so doing the actual logging in the calling thread is not feasible.

I can't find resources online that show how threading would work in log4net, if log4net supports this kind of message passing architecture already, or other similar features to work in a threaded environment. Are there any pre-existing features that would help me?

Is this possible without creating a log4net wrapper?


回答1:


You may want to rethink the threading approach if your log data depends on being in a specific order -- threading may interfere with that and end up posting log entries out of sequence.

You could try using MSMQ (or some other queue technology) to quickly post the log messages off to some other process which will then do the physical writes to storage. This will guarantee that messages appear in the same order they were sent.




回答2:


It's not a bad idea. The trick is to queue up the entries and process them in a single queue. Take a look at http://www.drdobbs.com/visualstudio/225700095 for a good example.




回答3:


You can always look at the source code for log4net to bottom out these sorts of questions. It is open source.




回答4:


If you use .NET Framework 4 you can use BlockingCollection to keep log sequence




回答5:


I've used ParallelForwardingAppender and AsyncForwardingAppender appenders with great success. These appenders are available from the following repository:

https://github.com/cjbhaines/Log4Net.Async

ParallelForwardingAppender utilizes BlockingCollection and other facets of the Task Parallel Library to implement a loss-less message queue.

AsyncForwardingAppender utilizes a ring buffer and a 10ms polling period on the background thread to prioritizes application performance over logging fidelity.



来源:https://stackoverflow.com/questions/3119494/running-log4net-appenders-on-separate-thread

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