通过 XML 文件*和*代码配置 log4net 附加程序

发布于 2024-08-31 16:10:42 字数 1360 浏览 9 评论 0原文

我今天开始使用 log4net,到目前为止,我真的很喜欢它。为了保留我们当前的日志记录功能,应用程序需要在应用程序启动时创建一个新的日志文件。日志文件名中包含编码的日期和时间戳。目前,我已经通过 XmlConfigurator 配置了 log4net,效果很好,只是我的 RollingFileAppender 的文件名硬编码在配置 XML 文件中。

我想继续使用 XmlConfigurator,但在调用 Configure() 后,我想获取 RollingFileAppender 并且在代码中,将其文件值更改为动态生成的字符串。示例在线文档现在似乎已关闭,但我浏览了 SDK 参考,看起来我可以使用 HeirarchyGetAppenders() 来完成我需要做的事情。我走在正确的轨道上吗?

好吧,我尝试了一下并尝试了以下代码,但它不起作用:

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 技术交流群。

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

发布评论

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

评论(2

友谊不毕业 2024-09-07 16:10:42

试试这个片段:

XmlConfigurator.Configure();

log4net.Repository.ILoggerRepository repo = LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repo.GetAppenders())
{
if (appender.Name.CompareTo("RollingFileAppender") == 0 && appender is log4net.Appender.RollingFileAppender)
{
   var appndr = appender as log4net.Appender.RollingFileAppender;
   string logPath = "MyApplication.log";
   appndr.File = logPath;
   appndr.ActivateOptions();
}

我在这里发布了类似的文章

Try this snippet:

XmlConfigurator.Configure();

log4net.Repository.ILoggerRepository repo = LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repo.GetAppenders())
{
if (appender.Name.CompareTo("RollingFileAppender") == 0 && appender is log4net.Appender.RollingFileAppender)
{
   var appndr = appender as log4net.Appender.RollingFileAppender;
   string logPath = "MyApplication.log";
   appndr.File = logPath;
   appndr.ActivateOptions();
}

I had posted similar article here

往日情怀 2024-09-07 16:10:42

在这种情况下,您需要滚动文件附加器吗?如果不是,我希望如果您使用普通文件附加器,您的代码将创建所需的结果。

编辑:如果您在附加器上调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文