¿Cómo uso una propiedad GlobalContext en un nombre de appender log4net?


Estoy tratando de personalizar una ruta de archivo log4net para usar una propiedad que he establecido en el diccionario log4net.GlobalContext.Properties.

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

Puedo ver que este valor se establece correctamente al depurar a través de él. y luego en mi configuración

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

Sin embargo, la salida de esto me da " _(null).log " al final del camino. ¿Qué pasa?

 38
Author: Anthony Mastrean, 2009-02-18

4 answers

Me encontré con el mismo comportamiento y lo resolví configurando la variable global antes de llamar al XmlConfigurator... Esto es lo que estoy usando con éxito:

Log4net.detalles de configuración:

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

Global.detalles de 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");
}

Espero que esto ayude...

 55
Author: Dscoduc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-21 04:12:53

Añadir type = log4net.Util.PatternString en el elemento de archivo

 16
Author: Noctis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-07 23:27:24

El problema( creo) es que obtiene(getLogger) el registrador antes de establecer el nombre y cargar la configuración...

Intenta declarar el registrador como: private static log4net.ILog _pLog y luego en el Application_Start hacer:

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

Así que la secuencia es:

// Set logfile name and application name variables
// Load log4net configuration
// get the logger
// Record application startup
 4
Author: Noctis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-07 23:28:13

¿ Se ha inicializado el logger a través del método global o main en la aplicación? Podría ser que el GlobalContext aún no se haya inicializado.

 1
Author: Dillie-O,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-18 17:54:39