记录到一个单独的日志文件,为每个线程 [英] Logging to an individual log file for each individual thread
问题描述
我有一个服务的应用程序,在启动时读取XML文件,并启动一个线程为XML文件中的每个条目。每个线程创建一个工人类这需要一个记录器,以任何输出记录到一个线程特定日志文件的一个实例。
I have a service application that on startup reads an XML file and starts a thread for each entry in the XML file. Each thread creates an instance of a worker class which requires a logger to log any output to a thread specific log file.
在我的app.config已设置使用XML的appender log4net的配置设置和文件被指定为PatternString如下服务:
In the services app.config I have the log4net configuration settings set to use an XML appender and the file is specified as a PatternString as shown below:
<appender name="XmlAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="D:\Temp\Logs\%property{LogName}.log" />
<immediateFlush value="true"/>
<appendToFile value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
在所创建我得到使用 log4net.LogManager.GetLogger(MyLogger)
方法,然后我把记录器工人阶级的每个实例线程锁定的方法当前线程使用PatternStrings LOGNAME属性 ThreadContext.Properties [LOGNAME] =日志名称preFIX
。
In a thread locked method for each instance of the worker class created I get the logger using the log4net.LogManager.GetLogger("MyLogger")
method and then I set the current threads PatternStrings LogName property using ThreadContext.Properties["LogName"] = "Log name prefix"
.
所有的文件被创建,但在记录器被称为它只是记录所有发送给一个看似随机的文件。
All of the files are created but when the logger is called it just logs all message to one seemingly random file.
我已搜查约了好一阵子试图找到一个解决办法,或一些答案什么我做错了,但我有没有运气。
I have searched about for quite a while trying to find a solution or some answers to what I am doing wrong but I have had no luck.
有没有人有任何想法,为什么发生这种情况?
Does anyone have any idea why this is happening?
推荐答案
我想我已经摸索出了问题。该步骤如下:
I think I have worked out the issue. The steps follow:
- 创建一个名为LoggerRepository每个线程上的个人。
- 将ThreadContexts属性的日志文件名。
- 使用XmlConfiguratior配置资源库。
- 使用日志管理使用命名LoggerRepository该线程获取指定的记录器(XML配置文件中)。
在回报,我得到一个新的配置记录指向该线程相应的文件。
In return I get a new configured logger pointing to the respective file for that thread.
XML配置是相同的,因为它是最初和这里示出为了完整性:
The XML configuration is the same as it was originally and shown here for completeness:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<logger name="ProductionLogger">
<appender-ref ref="XmlAppender"/>
<level value="ALL"/>
</logger>
<appender name="XmlAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="D:\Temp\Logs\%property{LogName}.log" />
<immediateFlush value="true"/>
<appendToFile value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
</log4net>
</configuration>
在code创建伐木者如下。每次这个code运行它在自己的线程中运行。
The code to create the loggers is below. Each time this code is run it is run in its own thread.
ILoggerRepository loggerRepository = LogManager.CreateRepository(logFileName + "Repository");
ThreadContext.Properties["LogName"] = logFileName;
log4net.Config.XmlConfigurator.Configure(loggerRepository);
ILog logger = LogManager.GetLogger(logFileName + "Repository", "ProductionLogger");
这似乎到目前为止,没有问题需要解决。我期待这个解决方案的时刻进行移动,但是如果我发现了什么事我会更新这个帖子。
This seems to work with no issues so far. I will be moving forward with this solution for the moment but I will update this post if I find out anything else.
这篇关于记录到一个单独的日志文件,为每个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!