title: log4net: How to set logger file name dynamically?
body: <p>This is a really common question, but I have not been able to get an answer to work.  Here is my configuration file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;log4net&gt;
    &lt;appender name="RollingFile" type="log4net.Appender.RollingFileAppender"&gt;
      &lt;file value="CraneUserInterface.log" /&gt;
      &lt;appendToFile value="true" /&gt;
      &lt;maxSizeRollBackups value="90" /&gt;
      &lt;rollingStyle value="Size" /&gt;
  
      &lt;layout type="log4net.Layout.PatternLayout"&gt;
        &lt;conversionPattern value="%date - %message%newline" /&gt;
      &lt;/layout&gt;
    &lt;/appender&gt;
  
    &lt;root&gt;
      &lt;level value="DEBUG" /&gt;
      &lt;appender-ref ref="RollingFile" /&gt;
    &lt;/root&gt;
  </code></pre>
  
  <p>But I need to determine the actual logging file name at run time.  I found a nice example <a href="http://www.l4ndash.com/News/tabid/69/ctl/ArticleView/mid/413/articleId/17/Programmatically-changing-log-file-names-in-Log4net.aspx">here</a>, but when I try to loop through the collection returned by the call to GetIterators(), I find that that collection is empty.</p>
  
  <p>I need to change the name "CraneUserInterface.log" to "CraneUserInterface_1.log", or 2, or 3, depending on something the program reads at run time.  How can I do that?</p>
  
  <p>Here's my first pass at using the code presented in that sample:</p>
  
  <pre class="lang-cs prettyprint-override"><code>static bool ChangeLogFileName(string AppenderName, string NewFilename)
  {           
     // log4net.Repository.ILoggerRepository RootRep;
     // RootRep = log4net.LogManager.GetRepository();
     log4net.Repository.ILoggerRepository RootRep = m_logger.Logger.Repository;
     foreach (log4net.Appender.IAppender iApp in RootRep.GetAppenders())
     {
     string appenderName = iApp.Name;
     if (iApp.Name.CompareTo(AppenderName) == 0
         &amp;&amp; iApp is log4net.Appender.FileAppender)
     {
         log4net.Appender.FileAppender fApp = (log4net.Appender.FileAppender)iApp;
         fApp.File = NewFilename;
         fApp.ActivateOptions();
         return true; // Appender found and name changed to NewFilename
     }
  }
  return false; // appender not found
  }
  </code></pre>
  
  <p>Thanks very much!</p>