在单独的线程上运行 Log4Net 附加程序
目前,我有自己的日志系统,其中日志本质上是一个队列,有一个单独的线程侦听该队列并执行所有实际的写入操作。系统处理大量日志消息,文件在几分钟内很容易超过 10 MB 大小,因此在调用线程中进行实际日志记录是不可行的。
如果 log4net 已经支持这种消息传递架构,或者在线程环境中工作的其他类似功能,我无法在网上找到显示线程如何在 log4net 中工作的资源。是否有任何现有功能可以帮助我?
如果不创建 log4net 包装器,这可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是一个坏主意。诀窍是将条目排队并在单个队列中处理它们。查看 http://www.drdobbs.com/visualstudio/225700095 以获得良好的效果例子。
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.
您始终可以查看 log4net 的源代码来解决此类问题。它是开源的。
You can always look at the source code for log4net to bottom out these sorts of questions. It is open source.
如果您使用 .NET Framework 4,则可以使用 BlockingCollection 来保持日志顺序
If you use .NET Framework 4 you can use BlockingCollection to keep log sequence
我使用了 ParallelForwardingAppender 和 AsyncForwardingAppender 附加器并取得了巨大成功。这些附加程序可从以下存储库中获取:
https://github.com/cjbhaines/Log4Net.Async
ParallelForwardingAppender
利用 BlockingCollection 和任务并行库的其他方面来实现无损消息队列。AsyncForwardingAppender
在后台线程上利用环形缓冲区和 10ms 轮询周期,将应用程序性能优先于日志保真度。I've used
ParallelForwardingAppender
andAsyncForwardingAppender
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.如果您的日志数据依赖于特定的顺序,您可能需要重新考虑线程方法 - 线程可能会干扰该顺序并最终以不按顺序发布日志条目。
您可以尝试使用MSMQ(或其他一些队列技术)将日志消息快速发布到其他进程,然后由该进程对存储进行物理写入。这将保证消息按照发送的顺序显示。
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.