如何使用在log4net的附加目的地名称GlobalContext中的财产? [英] How do I use a GlobalContext property in a log4net appender name?

查看:824
本文介绍了如何使用在log4net的附加目的地名称GlobalContext中的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义log4net的文件路径使用我在 log4net.GlobalContext.Properties 词典设置属性。

I'm trying to customise a log4net file path to use a property I have set in the log4net.GlobalContext.Properties dictionary.

log4net.GlobalContext.Properties["LogPathModifier"] = "SomeValue";

我可以看到,通过它在调试时该值是否设置正确。然后在我的配置

I can see that this value is set correctly when debugging through it. and then in my configuration

<file type="log4net.Util.PatternString" 
      value="Logs\%appdomain_%property{LogPathModifier}.log" />

然而,这个输出给我_(空)的.log在路径的末端。是什么给了?

However, the output of this gives me "_(null).log" at the end of the path. What gives?

推荐答案

我遇到了相同的行为,并通过调用XmlConfigurator之前设置的全局变量解决它......这就是我成功地使用:

I ran into the same behavior and solved it by setting the global variable before calling the XmlConfigurator... Here is what I am successfully using:

log4net.config细节:

log4net.config details:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
  <File type="log4net.Util.PatternString" value="App_Data/%property{LogName}" />
  ...
</appender>

Global.asax中的细节:

Global.asax details:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Global.asax");
void Application_Start(object sender, EventArgs e) 
{
    // Set logfile name and application name variables
    log4net.GlobalContext.Properties["LogName"] = GetType().Assembly.GetName().Name + ".log";
    log4net.GlobalContext.Properties["ApplicationName"] = GetType().Assembly.GetName().Name;

    // Load log4net configuration
    System.IO.FileInfo logfile = new System.IO.FileInfo(Server.MapPath("log4net.config"));
    log4net.Config.XmlConfigurator.ConfigureAndWatch(logfile);

    // Record application startup
    log.Debug("Application startup");
}

希望这有助于...

Hope this helps...

这篇关于如何使用在log4net的附加目的地名称GlobalContext中的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆