title: Can the log file path be changed on the fly (reguarly)
body: <p>I've been experimenting with log4net as it appears that it does not support a particular logging feature my project needs. <strong>In short, I want to be able to control the log file path in code.</strong> This path will change constantly. The specific use case is a set of file system watchers, and a separate log file is required per instance.</p>
  
  <p>Can this be done?</p>
  
  <p>I want to be able to specify a variable that controls the logging destination in code. </p>
  
  <p>For example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var log4NetLogger1 = new Log4NetLogger("LogFileAppender1");
  
  log4NetLogger1.InformationEvent("Log message 1");
  
  var log4NetLogger2 = new Log4NetLogger("LogFileAppender2");
  
  log4NetLogger2.InformationEvent("Log message 2");
  </code></pre>
  
  <p>In the above example, I am passing a string to the log4net wrapper, that matches an appender name in configuration. The idea is that the log4net wrapper changes the logging target by modifying the appender in use via GetLogger.</p>
  
  <pre class="lang-cs prettyprint-override"><code>_log = log4net.LogManager.GetLogger(appenderName);
  </code></pre>
  
  <p>The result is that two different log files are created, but the log message is written to both. It appears that some aspect of the log4net configuration is global, and I am not able to change log path on the fly in this way.</p>
  
  <p><strong>Configuration file:</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/&gt;
    &lt;/configSections&gt;
  
    &lt;log4net&gt;
  
      &lt;root&gt;
        &lt;appender-ref ref="LogFileAppender1"/&gt;
        &lt;appender-ref ref="LogFileAppender2"/&gt;
      &lt;/root&gt;
  
      &lt;appender name="LogFileAppender1" type="log4net.Appender.RollingFileAppender"&gt;
        &lt;threshold value="INFO"/&gt;
        &lt;param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog1.txt"/&gt;
        &lt;param name="AppendToFile" value="true"/&gt;
        &lt;rollingStyle value="Size"/&gt;
        &lt;maxSizeRollBackups value="10"/&gt;
        &lt;maximumFileSize value="10MB"/&gt;
        &lt;staticLogFileName value="true"/&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
          &lt;param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;appender name="LogFileAppender2" type="log4net.Appender.RollingFileAppender"&gt;
        &lt;threshold value="INFO"/&gt;
        &lt;param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog2.txt"/&gt;
        &lt;param name="AppendToFile" value="true"/&gt;
        &lt;rollingStyle value="Size"/&gt;
        &lt;maxSizeRollBackups value="10"/&gt;
        &lt;maximumFileSize value="10MB"/&gt;
        &lt;staticLogFileName value="true"/&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
          &lt;param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
  
    &lt;/log4net&gt;
  
  &lt;startup&gt;&lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/&gt;&lt;/startup&gt;&lt;/configuration&gt;
  </code></pre>
  
  <p><strong>Full wrapper class:</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using log4net;
  
  namespace Log4NetRunner
  {
      public enum LoggingLevel
      {
          Information,
          Warning,
          Error
      }
  
      public class Log4NetLogger
      {
          private readonly ILog _log;
  
          public Log4NetLogger(Type type)
          {
              if (_log == null)
              {
                  _log = log4net.LogManager.GetLogger(type);
              }
          }
  
          public Log4NetLogger(string appenderName)
          {
              if (_log == null)
              {
  
                  _log = log4net.LogManager.GetLogger(appenderName);
              }
          }
  
          public void FatalErrorEvent(string messageText)
          {
              SendLog(messageText, LoggingLevel.Error);
          }
  
          public void WarningEvent(string messageText)
          {
              SendLog(messageText, LoggingLevel.Warning);
          }
  
          public void InformationEvent(string messageText)
          {
              SendLog(messageText, LoggingLevel.Information);
          }
  
          private void SendLog(string messageText, LoggingLevel logLevel)
          {
              ILog logger = _log;
  
              switch (logLevel)
              {
                  case LoggingLevel.Error:
                      logger.Error(messageText);
                      break;
                  case LoggingLevel.Warning:
                      logger.Warn(messageText);
                      break;
                  case LoggingLevel.Information:
                      logger.Info(messageText);
                      break;
                  default:
                      logger.Error("Unknown Logging level: " + messageText);
                      break;
              }
          }
      }
  }
  </code></pre>