当动态设置日志文件名时,RollingFileAppender与log4net.Appender.FileAppender MinimalLock锁定模型一起使用
似乎没有记录任何文件.但是,在没有此锁定模型的情况下使用appender时,日志记录有效.
在下面的配置文件中,Appender1可以工作,但Appender2不能:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="Appender2"
type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString" value="%property{LogName}" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%logger] %message%n" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="Appender1"
type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString" value="%property{LogName}" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%n" />
</layout>
</appender>
<logger name="myLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="Appender2" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="Appender1" />
</root>
</log4net>
</configuration>
这是我正在测试的示例代码:
static void Main(string[] args)
{
ILog logger = LogManager.GetLogger("myLogger");
GlobalContext.Properties["LogName"] = "Log1.log"; // <== The log file name is getting set here.
log4net.Config.XmlConfigurator.Configure(new FileInfo(@"Logger.config"));
logger.Info("Sample log entry");
}
我做的另一个观察是,如果我删除根级别记录器,则记录似乎有效.
解决方法:
我想你必须添加
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
对于两个appender,Appender1中都没有.这将解释为什么删除根元素有效.