【发布时间】:2010-05-11 00:26:08
【问题描述】:
我今天开始玩 log4net,到目前为止,我真的很喜欢它。为了保留我们当前的日志记录功能,应用程序需要在应用程序启动时创建一个新的日志文件。日志文件名中包含编码的日期和时间戳。目前,我已经通过XmlConfigurator 配置了 log4net,效果很好,只是我的RollingFileAppender 的文件名是硬编码在配置 XML 文件中的。
我想继续使用XmlConfigurator,但在调用Configure() 之后,我想访问RollingFileAppender,并在代码中将其文件值更改为动态生成的字符串。示例documentation online 现在似乎已经关闭,但我已经浏览了 SDK 参考,看起来我可以使用Heirarchy 和GetAppenders() 来做我需要做的事情。我在正确的轨道上吗?
好的,我尝试了一下,尝试了以下代码,但没有成功:
private static readonly ILog _log = LogManager.GetLogger(typeof(GUI));
// in the config file, I've set the filename to example.log, and it works
XmlConfigurator.Configure(new FileInfo("log_config.xml"));
Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
if(hierarchy != null) {
// get the appenders
IAppender[] appenders = hierarchy.GetAppenders();
// change the filename for the RollingFileAppender
foreach( IAppender a in appenders) {
RollingFileAppender rfa = a as RollingFileAppender;
if(rfa == null)
continue;
rfa.File = "newfile.log"; // no runtime error, but doesn't work.
}
}
_log.Info("Application started");
【问题讨论】:
标签: log4net appender rollingfileappender log4net-configuration