RollingFileAppender 中动态文件命名的 log4net 问题
我的配置文件中有 3 个附加程序,用于创建 3 种不同类型的日志。我通过设置全局上下文属性在 3 个附加程序中的每一个中使用文件的动态命名。在某些情况下,我需要为 1 个附加程序动态设置日志文件名。
当我仅为 1 个附加程序设置文件名时,除了动态设置名称的实际日志文件之外,它还会创建另一个名为“null”的文件,其中没有任何数据。 我已经创建了如图所示的配置文件。
<appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%property{applog}" />
.
.
.
<appender name="RollingFileAppenderV2" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%property{dblog}" />
.
.
.
<logger name="Logger1">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppenderV1" />
</logger>
<logger name="Logger2">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppenderV2" />
</logger>
在 VB.NET 代码中,我将文件名设置为:
log4net.GlobalContext.Properties("applog") = "file1.log"
Dim logobj as log4net.Ilog = LogManager.GetLogger("Logger1")
logobj.debug("test")
在本例中,它创建“file1.log”以及另一个名称为“null”的空文件。仅当我在运行时设置任一附加程序文件名时才会发生这种情况。 任何帮助表示赞赏。
I have 3 appenders in my config file for creating 3 different types of logs. I am using dynamic naming of file in each of the 3 appenders by setting the global context properties. In some cases, i need to set the log file name dynamically for just 1 appender.
When i set the file name for just 1 appender, it creates another file named "null" with no data in addition to the actual logfile whose name has been set dynamically.
I have created the config file as shown.
<appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%property{applog}" />
.
.
.
<appender name="RollingFileAppenderV2" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%property{dblog}" />
.
.
.
<logger name="Logger1">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppenderV1" />
</logger>
<logger name="Logger2">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppenderV2" />
</logger>
In the VB.NET code i set the filename as :
log4net.GlobalContext.Properties("applog") = "file1.log"
Dim logobj as log4net.Ilog = LogManager.GetLogger("Logger1")
logobj.debug("test")
In this case it creates "file1.log" and also another empty file with name as "null". This happens only when i am setting either of the appenders filename at runtime.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,日志文件是在配置 log4net 时创建的。这意味着您需要确保先设置该属性,然后再配置 log4net。
例如
As far as I know the log file is created the moment you configure log4net. This means that you need to make sure to set the property first and configure log4net afterwards.
e.g.
我有完全相同的问题。调用 XmlConfigurator.Configure() 创建了两个文件 - 一个具有正确的文件路径,另一个具有运行程序集的文件夹中的“null”命名文件。
我通过在第一次调用 XmlConfigurator.Configure() 之前将所有文件路径属性初始化为 string.Empty 解决了这个问题。
因此,对于您的情况,以下代码应该可以解决该问题:
I had exactly the same problem. Calling XmlConfigurator.Configure() created two files - one with the correct file path and the other with 'null' named file in the folder from which the assembly runs.
I solved this by initializing all of my file path properties to string.Empty before calling XmlConfigurator.Configure() for the first time.
So, in your case, the following code should solve the issue:
log4net github 页面上有关于此事的讨论:
https://github。 com/net-commons/common-logging/issues/81
我不会解释整个讨论,但建议的解决方法是指示 log4net 显式重置其配置,直到可以动态覆盖属性:
There is discussion about this very thing on the log4net github page:
https://github.com/net-commons/common-logging/issues/81
I won't paraphrase the entire discussion, but the suggested workaround, until properties can be overridden dynamically, is to instruct log4net to reset its configuration explicitly: