title: Best way to dynamically set an appender file path
body: <p>I am trying to find somebody smarter than me to validate some syntax I wrote up.  The idea is to configure the filename of my RollingFileAppender to the name of the assembly in order to make it more re-usable for my projects.  </p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/308436/log4net-programmatcially-specify-multiple-loggers-with-multiple-file-appenders">this previous SO article</a> but it wasn't exactly able to answer my question...</p>
  
  <p>I've had a dickens of a time trying to understand the inner components of Log4net and this is what I came up with (residing in the Global.asax file - Application_Start method):</p>
  
  <pre><code>// Bind to the root hierarchy of log4net
  log4net.Repository.Hierarchy.Hierarchy root = 
    log4net.LogManager.GetRepository() 
      as log4net.Repository.Hierarchy.Hierarchy;
  
  if (root != null)
  {
    // Bind to the RollingFileAppender
    log4net.Appender.RollingFileAppender rfa = 
      (log4net.Appender.RollingFileAppender)root.Root.GetAppender("RollingLogFileAppender");
  
    if (rfa != null)
    {
      // Set the file name based on the assembly name
      string filePath = 
        string.Format("~/App_Data/{0}.log", GetType().Assembly.GetName().Name);
  
      // Assign the value to the appender
      rfa.File = Server.MapPath(filePath);
  
      // Apply changes to the appender
      rfa.ActivateOptions();
    }
  }
  </code></pre>
  
  <p>Can anyone tell me, 'this is hideous', or 'this should work fine'?  Also, if I set the file dynamically can I still expect the log4net behavior to rotate the files based on the log4net.config file settings?</p>
  
  <p>Much appreciated!</p>