简体   繁体   English

写入具有多线程性能问题的log4net FileAppender

[英]Writing to a log4net FileAppender with multiple threads performance problems

TickZoom is a very high performance app which uses it's own parallelization library and multiple O/S threads for smooth utilization of multi-core computers. TickZoom是一款性能非常高的应用程序,它使用自己的并行化库和多个操作系统线程来平滑地利用多核计算机。

The app hits a bottleneck where users need to write information to a LogAppender from separate O/S threads. 该应用程序遇到瓶颈,用户需要从单独的O / S线程向LogAppender写入信息。

The FileAppender uses the MinimalLock feature so that each thread can lock and write to the file and then release it for the next thread to write. FileAppender使用MinimalLock功能,以便每个线程可以锁定并写入该文件,然后释放它以供下一个要写入的线程使用。

If MinimalLock gets disabled, log4net reports errors about the file being already locked by another process (thread). 如果MinimalLock被禁用,log4net将报告有关已被另一个进程(线程)锁定的文件的错误。

A better way for log4net to do this would be to have a single thread that takes care of writing to the FileAppender and any other threads simply add their messages to a queue. log4net执行此操作的更好方法是使用单个线程来处理写入FileAppender,而任何其他线程只是将其消息添加到队列中。

In that way, MinimalLock could be disabled to greatly improve performance of logging. 这样,可以禁用MinimalLock以大大提高日志记录的性能。

Additionally, the application does a lot of CPU intensive work so it will also improve performance to use a separate thread for writing to the file so the CPU never waits on the I/O to complete. 此外,该应用程序执行大量CPU密集型工作,因此它还可以提高性能,使用单独的线程写入文件,以便CPU永远不会等待I / O完成。

So the question is, does log4net already offer this feature? 所以问题是,log4net是否已经提供此功能? If so, how do you do enable threaded writing to a file? 如果是这样,你如何启用线程写入文件? Is there another, more advanced appender, perhaps? 是否还有另一个更高级的追加者?

If not, then since log4net is already wrapped in the platform, that makes it possible to implement a separate thread and queue for this purpose in the TickZoom code. 如果没有,那么由于log4net已经包含在平台中,因此可以在TickZoom代码中为此目的实现单独的线程和队列。

Sincerely, Wayne 真诚的,韦恩

EDIT: 编辑:

Thanks it seems the answers point to developing our own solution as perhaps an extension to log4net in some way. 谢谢,似乎答案指向开发我们自己的解决方案,可能是某种方式的log4net的扩展。 And they clearly show log4net doesn't do this type of thing. 他们清楚地表明log4net不会做这类事情。

Furthermore, we just realized that we might be "abusing" the logging system which is mainly meant for human readable messages for notifying of important events or debugging information. 此外,我们刚刚意识到我们可能“滥用”日志系统,该系统主要用于通知重要事件或调试信息的人类可读消息。 This particular part of the software output is only really used for automated tools that verify the accuracy of the system. 软件输出的这一特定部分仅用于验证系统准确性的自动化工具。

Of course, we also use log4net in the "normal" ways for debugging, warnings, and such. 当然,我们也以“正常”方式使用log4net进行调试,警告等。

But these are more like "transaction logs" than debug or user notification logs. 但这些更像是“事务日志”,而不是调试或用户通知日志。 More specifically, it's unnecessary for these logs to be directly human readable. 更具体地说,这些日志不必直接是人类可读的。 If necessary a "viewer" of some sort can show the contents in ASCII form. 如果需要,某种“查看器”可以以ASCII格式显示内容。

So we will plan on making these transaction-type log get written to a high speed binary storage. 因此我们计划将这些事务类型的日志写入高速二进制存储。

Thanks it seems both of the answers below were great nudges toward developing our own solution. 谢谢,以下两个答案似乎都非常适合开发我们自己的解决方案。

Log4net doesn't support the exact scenario you describe. Log4net不支持您描述的确切方案。 It does however offer other appenders that don't lock, like the database appender, or the UDP appender. 但它提供了其他不锁定的appender,如数据库appender或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. 说实话,我不知道是否已经有一个MSMQ appender,但自己写它不会太难。

  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. 使用UDP appender发送消息,然后编写自己的服务来侦听这些消息并将它们写入文件。

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. 我认为你可以在这里检测一个主题......基本上使用一个非阻塞appender(或编写你自己的)并实现你自己的服务,它接收来自appender的消息并将它们写入文件。

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/ . 查看Object Guy的记录器,获得高性能,多线程安全记录器,具有异步记录功能以及许多其他功能 - 我认为非常好 - 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 / O,因此向日志记录收集器线程发送消息是个好主意。 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. 我有时也会使用目标线程中的System :: Diagnostics :: Debug :: WriteLine来转储它的输出,但无论如何都必然会有一点锁定。

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. 当然,添加任何额外的日志记录将导致“Heisenberg”效果,因此您必须以某种方式知道这些影响何时可以忽略不计,何时不能,以便有效记录您的高性能线程。

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. 因此,数据仅在任何线程执行其日志记录I / O之前的一段时间内有用,但也许您可以捕获足够的信息以进行调试。 Also, you get the fun task of collating the individual thread logs into a single log for analysis. 此外,您还可以将各个线程日志整理到一个日志中进行分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM