通过 XML 文件*和*代码配置 log4net 附加程序
我今天开始使用 log4net,到目前为止,我真的很喜欢它。为了保留我们当前的日志记录功能,应用程序需要在应用程序启动时创建一个新的日志文件。日志文件名中包含编码的日期和时间戳。目前,我已经通过 XmlConfigurator
配置了 log4net,效果很好,只是我的 RollingFileAppender
的文件名硬编码在配置 XML 文件中。
我想继续使用 XmlConfigurator
,但在调用 Configure()
后,我想获取 RollingFileAppender
并且在代码中,将其文件值更改为动态生成的字符串。示例在线文档现在似乎已关闭,但我浏览了 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");
I started to play with log4net today and so far, I really like it. In order to preserve our current logging functionality, the app needs to create a new log file whenever the application is started. The log file name has the date and time stamp encoded in it. Currently, I've got log4net configured via an XmlConfigurator
, which works great, except that the filename for my RollingFileAppender
is hardcoded in the configuration XML file.
I'd like to continue to use the XmlConfigurator
, but after calling Configure()
, I want to get at the RollingFileAppender
and, in code, change its file value to be a dynamically-generated string. The sample documentation online seems to be down right now, but I've poked through the SDK reference, and it looks like I could use the Heirarchy
and GetAppenders()
to do what I need to do. Am I on the right track?
Ok, I took a stab at this and tried the following code, which didn't work:
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个片段:
我在这里发布了类似的文章
Try this snippet:
I had posted similar article here
在这种情况下,您需要滚动文件附加器吗?如果不是,我希望如果您使用普通文件附加器,您的代码将创建所需的结果。
编辑:如果您在附加器上调用
ActivateOptions()
,也许它可以与 RollingFile Appender 一起使用。Do you in this case need the rolling file appender? If not I would expect that your code would create the desired result if you used the normal file appender.
Edit: Maybe it works with the RollingFile Appender if you call
ActivateOptions()
on the appender.