如何使用 log4net 将同一应用程序的不同实例记录到不同的文件?
我的问题与这个问题类似,但上下文更简单。就我而言,我有一个应用程序,它可以使用不同的命令行参数同时运行两次(最多),因此每个实例都知道其上下文。
app.config 文件定义 log4net 部分来配置日志记录。当然
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="MainAppender" type="log4net.Appender.FileAppender">
<file value="${TMP}\NCAgent.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="MainAppender" />
<level value="INFO"/>
<appender-ref ref="DebugAppender"/>
</root>
</log4net>
,我们有一个问题,两个应用程序实例可能会记录到同一个日志文件,因此在运行时,其中一个实例(选择定义明确且明确)加载 log4net XmlElement,更改日志文件的名称并使用
XmlConfigurator.Configure(XmlElement)
重载配置 log4net。
我不喜欢我们的做法,并认为应该有更好的解决方案。有什么想法吗?
谢谢。
My question is similar to this one, but with a somewhat simpler context. In my case I have a single application which may be run twice (at most) simultaneously with different command line parameters, so each instance knows its context.
The app.config file defines a log4net section to configure logging. Here it is:
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="MainAppender" type="log4net.Appender.FileAppender">
<file value="${TMP}\NCAgent.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="MainAppender" />
<level value="INFO"/>
<appender-ref ref="DebugAppender"/>
</root>
</log4net>
And, of course, we have a problem that two application instances may log to the same log file, so at runtime one of the instances (the choice is well defined and unambiguous) loads the log4net XmlElement
, changes there the name of the log file and configures log4net using the XmlConfigurator.Configure(XmlElement)
overload.
I do not like our approach and feel that there should be a better solution. Any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以编程方式配置 FileAppender 文件名并附加进程 ID,或者子类化 FileAppender 以使用基于配置的方法自动执行上述行为。
You could try either configuring the FileAppender filename programmatically and appending the process ID, or subclass the FileAppender to make said behavior automatic with the config-based approach.
请参阅类似问题的答案。
您可以定义文件名的模式,包括唯一的内容,例如时间戳或进程 ID。
See this answer to a similar question..
You can define a pattern for the filename including something unique such as a timestamp or a process id.