将Log4net用于多线程应用程序 [英] Using log4net for multithreaded application

查看:624
本文介绍了将Log4net用于多线程应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用.net线程池的应用程序有多个线程,它使用log4net将日志写入纯文本文件.这样使用log4net进行异步日志记录是一个好主意.还是我需要单独的MSMQ实现来附加消息?

I have a application which uses .net Thread-pool to have multiple threads.It uses log4net for write logs to a plain text file. Is it a good idea to use log4net for asynchronous logging like this. Or do i need to have separate MSMQ implementation to append messages?

推荐答案

您可以按原样使用log4net进行多线程应用程序的基于文件的日志记录.来自所有线程的日志消息将被写入同一文件.阅读所有散布的消息可能会造成一些混乱,但是总比没有记录好.您肯定要以附加程序格式记录线程ID,以便可以分辨出哪些消息来自哪个线程.

You can use log4net as-is for file-based logging for multi-threaded applications. The log messages from all the threads will be written to the same file. It can get a little confusing to read all the interspersed messages, but it's better than not having logging. You'll definitely want to log the thread ID in the appender format so you can tell which messages are coming from which thread.

您可以做更多花哨的事情来处理不同线程的日志记录,但是我从来没有真正走过这条路.我宁愿坚持基于文件的日志记录,并且我认为将所有线程日志记录到一个文件比将每个线程日志记录到其自己的文件更容易处理.

There are probably more fancy things you can do to handle the logging for different threads, but I've never really had to go down that road. I prefer to stick with file-based logging, and having all the threads log to one file is easier to deal with than having each thread log to its own file, in my opinion.

这篇关于将Log4net用于多线程应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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