RollingFileAppender 中动态文件命名的 log4net 问题

发布于 2024-09-25 19:47:15 字数 1144 浏览 0 评论 0原文

我的配置文件中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

上课铃就是安魂曲 2024-10-02 19:47:15

据我所知,日志文件是在配置 log4net 时创建的。这意味着您需要确保先设置该属性,然后再配置 log4net。

例如

log4net.GlobalContext.Properties["applog"] = "file1.log"
log4net.Config.XmlConfigurator.Configure();

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.

log4net.GlobalContext.Properties["applog"] = "file1.log"
log4net.Config.XmlConfigurator.Configure();
爱,才寂寞 2024-10-02 19:47:15

我有完全相同的问题。调用 XmlConfigurator.Configure() 创建了两个文件 - 一个具有正确的文件路径,另一个具有运行程序集的文件夹中的“null”命名文件。

我通过在第一次调用 XmlConfigurator.Configure() 之前将所有文件路径属性初始化为 string.Empty 解决了这个问题。

因此,对于您的情况,以下代码应该可以解决该问题:

log4net.GlobalContext.Properties("applog") = "file1.log"

' Set all of the other properties defined in the config file to String.Empty.
' By default, they are null and cause the issue.
log4net.GlobalContext.Properties("dblog") = String.Empty

log4net.Config.XmlConfigurator.Configure()

Dim logobj as log4net.Ilog = LogManager.GetLogger("Logger1")
logobj.debug("test")

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.GlobalContext.Properties("applog") = "file1.log"

' Set all of the other properties defined in the config file to String.Empty.
' By default, they are null and cause the issue.
log4net.GlobalContext.Properties("dblog") = String.Empty

log4net.Config.XmlConfigurator.Configure()

Dim logobj as log4net.Ilog = LogManager.GetLogger("Logger1")
logobj.debug("test")
紧拥背影 2024-10-02 19:47:15

log4net github 页面上有关于此事的讨论:

https://github。 com/net-commons/common-logging/issues/81

我不会解释整个讨论,但建议的解决方法是指示 log4net 显式重置其配置,直到可以动态覆盖属性:

// This line sets the properties in global context
var dummy = Common.Logging.LogManager.GetLogger("dummy");

// Now reset the ILoggingFactoryAppender to null so log4net needs to be re-configured
Common.Logging.LogManager.Reset(); 

// Now you can override properties in the global context
dummy.GlobalVariablesContext.Set("LogsDirectory", "C:\\Logs");

// Access to LogManager.GetLogger() triggers log4net initialization because the ILoggingFactoryAppender was set to NULL by LogManager.Reset().
var logger = Common.Logging.LogManager.GetLogger<MyLogger>();

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:

// This line sets the properties in global context
var dummy = Common.Logging.LogManager.GetLogger("dummy");

// Now reset the ILoggingFactoryAppender to null so log4net needs to be re-configured
Common.Logging.LogManager.Reset(); 

// Now you can override properties in the global context
dummy.GlobalVariablesContext.Set("LogsDirectory", "C:\\Logs");

// Access to LogManager.GetLogger() triggers log4net initialization because the ILoggingFactoryAppender was set to NULL by LogManager.Reset().
var logger = Common.Logging.LogManager.GetLogger<MyLogger>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文