【发布时间】:2012-03-26 05:58:29
【问题描述】:
是的,这个问题类似于: How to log into separate files per thread with Log4Net? 除非在运行时之前我不知道线程的数量或它们的名称。我的 Windows 应用程序为每个用户生成一个线程来为该用户执行长时间运行的工作。我希望每个用户/线程都有一个单独的日志文件。
- log4net 配置文件是什么样的(如果可以用于这种类型的东西)?
- 使用记录器的代码是什么样的?
- 我什么时候打电话给
log4net.Config.XmlConfigurator.Configure()?
(请详细说明如何实现日志记录。)
这是一个示例配置(我无法让 thread_name 属性与多个线程一起使用):
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--need to replace LogDir in code like this: log4net.GlobalContext.Properties["LogDir"] = "c:\programdata\myapp"-->
<file type="log4net.Util.PatternString" value="%property{LogDir}\logs\mylogfile_%property{thread_name}.log" />
...
还有代码:
public class MyMultiThreadedClassForUsers
{
private log4net.ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void Start()
{
log4net.GlobalContext.Properties("LogDir") = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
log4net.Config.XmlConfigurator.Configure()
List<IUser> users = GetAllUsersFromDB();
foreach (IUser user in users) {
System.Threading.Thread t = new System.Threading.Thread(CallBackMethod);
t.Name = user.FirstName;
t.Start();
}
}
private void CallBackMethod()
{
// this log message should be sent to a log file named after the current thread System.Threading.Thread.CurrentThread.Name
// Examples: mylogfile_bob.log, and mylogfile_fred.log, etc...
Log.Info("Starting work on thread " + System.Threading.Thread.CurrentThread.Name);
// do long running work here
}
}
如果使用 log4net 不容易做到这一点,我可以将日志框架切换到 Nlog 并使用它们的 %threadname 关键字作为存储在配置文件中的日志文件名的一部分。
【问题讨论】:
-
是的,这很容易做到。看这里:stackoverflow.com/questions/1324053/…。只需将文件名基于 threadid 或其他一些保证唯一的临时名称。
-
那行不通。它不断记录到 app.config 中指定的文件。你能给我一个有效的 .net 例子吗?
标签: .net multithreading logging log4net