Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a GlobalContext property in a log4net appender name?

Tags:

c#

log4net

People also ask

What is the use of context properties in log4net?

You can set the value of a context property to any object reference; the value of the object's ToString method will be used to obtain the context property value when a logging event occurs. This can be useful when you need a context property to represent a calculated state at the time of each logging event.

What is rolling file Appender in log4net?

RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.


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 details:

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

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


Add type=log4net.Util.PatternString into File element


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

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

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

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.