如何在log4net appender名称中使用GlobalContext属性?

时间:2021-11-14 22:01:28

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

我正在定制一个log4net文件路径,以使用我在log4net. globalcontext中设置的属性。属性字典。

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" />

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

然而,这个输出给了我“_(null)”。在路径的末端做记录。到底发生了什么事?

4 个解决方案

#1


54  

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:

我遇到了相同的行为,并通过在调用XmlConfigurator之前设置全局变量来解决它。以下是我成功使用的方法:

log4net.config details:

log4net。配置细节:

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

Global.asax details:

全球。asax细节:

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...

希望这有助于……

#2


14  

Add type=log4net.Util.PatternString into File element

添加类型= log4net.Util。PatternString成文件元素

#3


4  

The problem( I think) is that you GET(GetLogger) the logger before you set the name and load the config...

问题是(我认为)在设置名称和加载配置之前,您会得到(GetLogger)日志记录器……

Try to do declare the logger like: private static log4net.ILog _pLog and then in the Application_Start do:

尝试将日志记录器声明为:private static log4net。ILog _pLog然后在Application_Start中:

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);

    //Get the loger
    _pLog = log4net.LogManager.GetLogger("Global.asax");

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

So the sequence is:

顺序是:

// Set logfile name and application name variables
// Load log4net configuration
// get the logger
// Record application startup

#4


1  

Has the logger been initialized through the global or main method in the application? It could be that the GlobalContext has not been initialize yet.

是否已通过应用程序中的全局或主方法初始化日志记录器?可能是GlobalContext还没有初始化。

#1


54  

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:

我遇到了相同的行为,并通过在调用XmlConfigurator之前设置全局变量来解决它。以下是我成功使用的方法:

log4net.config details:

log4net。配置细节:

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

Global.asax details:

全球。asax细节:

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...

希望这有助于……

#2


14  

Add type=log4net.Util.PatternString into File element

添加类型= log4net.Util。PatternString成文件元素

#3


4  

The problem( I think) is that you GET(GetLogger) the logger before you set the name and load the config...

问题是(我认为)在设置名称和加载配置之前,您会得到(GetLogger)日志记录器……

Try to do declare the logger like: private static log4net.ILog _pLog and then in the Application_Start do:

尝试将日志记录器声明为:private static log4net。ILog _pLog然后在Application_Start中:

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);

    //Get the loger
    _pLog = log4net.LogManager.GetLogger("Global.asax");

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

So the sequence is:

顺序是:

// Set logfile name and application name variables
// Load log4net configuration
// get the logger
// Record application startup

#4


1  

Has the logger been initialized through the global or main method in the application? It could be that the GlobalContext has not been initialize yet.

是否已通过应用程序中的全局或主方法初始化日志记录器?可能是GlobalContext还没有初始化。