多线程安全日志记录 [英] Multithread safe logging

查看:1005
本文介绍了多线程安全日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经在多个线程运行,并使用log4net的日志记录框架的应用程序。我们遇到过一些日志事件不记录的情况。如文档提到的, FileAppender 和其他附加目的地都是<强>不可以安全的多线程操作。
我搜索解决方案,或附加目的地网络,但找不到任何。结果
你知道使用的环形缓冲区或队列提供多线程支持多线程安全的log4net的Appender的?或者我们应该用在所有不同的多线程安全的日志框架?结果
提前感谢!

We have an application that runs in multiple threads and uses Log4Net as logging framework. We encountered a scenario where some log events weren't logged. As mentioned in the docs, the FileAppender and the other Appenders are "not safe for multithreaded operations". I searched the web for solutions or Appenders, but couldn't find any.
Do you know a multithread safe Log4Net Appender that uses a ring buffer or a queue to provide multithread support? Or should we use a different multithread safe logging framework at all?
Thanks in advance!

推荐答案

我写了一些单元测试来重现问题:测试创建50个线程和每个线程记录500条消息。事后的书写线进行计数,其结果是我在不同顺序25000(50×500)行。我测试了它在双核和八核的机器上。
我测试了静态的记录器

I wrote some Unit tests to reproduce the problem: A test creates 50 threads and each thread logs 500 messages. Afterwards the written lines were counted and as a result I got 25,000 (50 x 500) lines in different order. I tested it on a dual core and on a eight core machine.
I tested a static Logger:

private static ILog StaticLog = log4net.LogManager.GetLogger(RepositoryName, "Static logger");

和一个记录器的测试类/线程的每个实例:

and with a Logger for each instance of the test class / thread:

ILog instanceLog = LogManager.GetLogger(RepositoryName, "Instance logger: " + ThreadId.ToString());




并所有的测试都是绿色的。


所以log4net的正常工作并处理多线程的情况很好。该追加程序文档应更新和状态,如果记录器API在以正确的方式使用多线程操作的支持。


我想有缺失的日志条目,我们对客户的遇到的问题机是由其他问题引起的。也许底层VM或硬件损坏。


感谢您的帮助!


And all tests were green.

So Log4Net works fine and handles multithreading scenarios well. The Appender docs should be updated and state that multithreaded operations are supported if the Logger API is used in the right way.

I guess the problem with missing log entries that we encountered on a customer's machine is caused by other issues. Maybe the underlying VM or hardware is broken.

Thanks for your help!

这篇关于多线程安全日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆