Log4net RollingFileAppender requiring a call to Configure () for each backup attempt

Not sure what is going on here, but my registration code will not be written to the rollFileAppender file unless I call XmlConfigurator.Configure () for each call. I turned on debugging in the following code and I can confirm that when Configure is called only once in the constructor, it seems to pull my configuration, but when Log () is actually called, nothing happens with the log file OR appears in the debug window . If I uncomment this call to Configure () in Log () and allow it to be reconfigured on every call, then it works fine, but I don't think that is the intended use.

Bonus points! - I noticed that the very first call to log.Info () is not logged, but all subsequent ones in every process start are fine.

Thank!

  public static class LogToFile
{
    public const string RollingFileAppenderName = "RollingFileLogger";

    static LogToFile()
    {
        log4net.Config.XmlConfigurator.Configure();
    }

    public static void Log(string fileNameBase, string message, string context)
    {
        if (fileNameBase == null) throw new ArgumentNullException("fileNameBase");
        if (message == null) throw new ArgumentNullException("message");
        if (context == null) throw new ArgumentNullException("context");

        //log4net.Config.XmlConfigurator.Configure();

        string fileName = string.Format("{0}_{1}.log", fileNameBase, context);
        string fullFileName = Path.Combine(Properties.Settings.Default.LogFilePath, fileName);

        if (!Directory.Exists(Properties.Settings.Default.LogFilePath)) Directory.CreateDirectory(Properties.Settings.Default.LogFilePath);

        LogicalThreadContext.Properties["LogName"] = string.Format(fullFileName);
        ILog log = LogManager.GetLogger(RollingFileAppenderName);

        log.Info(message);
    }
}

    <appender name="rollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="%property{LogName}" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="2MB" />
  <countDirection value="-1"/>
  <LockingModel value="log4net.Appender.FileAppender+MinimalLock"/>
  <staticLogFileName value="false" />
  <immediateFlush value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="AlertMessageHandler Message : %-25date [%thread] - %newline%message%newline" />
  </layout>
</appender>
+3
source share
3 answers

Actuall sgmoore tips led me to a solution. If I can just name the log file in the Appender object instead of working with the Property configuration file, then the ActivateOptions call works fine. Here is the working code. Thank!

public static class LogToFile
{
    public const string RollingFileLoggerName = "RollingFileLogger";

    public const string RollingFileAppenderName = "rollingFileAppender";

    private static readonly ILog Logger = LogManager.GetLogger(RollingFileLoggerName);

    /// <summary>
    /// Logs to log4net rollingFileAppender
    /// </summary>
    /// <param name="fileNameBase">prefix of log filename</param>
    /// <param name="message">log4net message property</param>
    /// <param name="context">files grouped by context</param>
    public static void Log(string fileNameBase, string message, string context)
    {
        if (fileNameBase == null) throw new ArgumentNullException("fileNameBase");
        if (message == null) throw new ArgumentNullException("message");
        if (context == null) throw new ArgumentNullException("context");

        string fileName = string.Format("{0}_{1}.log", fileNameBase, context);
        string fullFileName = Path.Combine(Properties.Settings.Default.LogFilePath, fileName);

        if (!Directory.Exists(Properties.Settings.Default.LogFilePath)) Directory.CreateDirectory(Properties.Settings.Default.LogFilePath);

        ActivateAppenderOptions(string.Format(fullFileName));

        Logger.Info(message);
    }

    /// <summary>
    /// Update the appender in log4net after setting the log file path
    /// </summary>
    /// <param name="filename"></param>
    private static void ActivateAppenderOptions(string filename)
    {
        var hierarchy = (Hierarchy)LogManager.GetRepository();
        var appenders = hierarchy.GetAppenders();
        foreach (
            var rfa in appenders.OfType<RollingFileAppender>().Where(rfa => rfa.Name == RollingFileAppenderName))
        {
            rfa.File = filename;
            rfa.ActivateOptions();
        }
    }
}
+1
source

, LogName, . , ( ActivateOptions) XmlConfigurator.Configure(), , .

, XmlConfigurator.Configure() before LogName, , ( null), , .

XmlConfigurator.Configure() , LogName, .

+2

:

private static readonly ILog log = LogManager.GetLogger(typeof(LogToFile));

, , . .

, , XmlConfigurator.Configure().

log4net.Config.XmlConfigurator.Configure(new FileInfo(configFile));
+1
source

All Articles