ingFileAppender中具有动态文件名的log​​4ne

ingFileAppender中具有动态文件名的log​​4ne

本文介绍了在RollingFileAppender中具有动态文件名的log​​4net中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置文件中有3个appender用于创建3种不同类型的日志。我通过设置全局上下文属性,在3个appender中的每一个中使用文件的动态命名。在某些情况下,我需要为1个appender动态设置日志文件名。

当我设置只有1个appender的文件名时,它会创建一个名为null的文件,没有数据除了名称已经动态设置的实际日志文件之外。
我已经创建了如图所示的配置文件。

 < appender name =RollingFileAppenderV1type =log4net.Appender.RollingFileAppender> 
< file type =log4net.Util.PatternStringvalue =Logs\%property {applog}/>



< appender name =RollingFileAppenderV2type =log4net.Appender.RollingFileAppender>
< file type =log4net.Util.PatternStringvalue =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的空文件。这仅在我在运行时设置任何一个appender文件名时才会发生。
任何帮助赞赏。

解决方案

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



例如

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


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.

解决方案

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();

这篇关于在RollingFileAppender中具有动态文件名的log​​4net中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:21