title: log4net: using PatternString in code, not in config file
body: <p>I would like to use the PatternString to specify a pattern in the filename for RollingFileAppender using log4net in C#. This can be easily done in the config file, and there are many examples of this online.  However, I would like to do the same in C# code.</p>
  
  <p>This is how it is done in the config file:</p>
  
  <pre><code> &lt;file type="log4net.Util.PatternString" value="C:\Log\MyLogFile.[%processid].txt" /&gt;
  </code></pre>
  
  <p>My code below does everything about setting up the log4net without a config file.
  Except the PatternString line above. How do I specify a PatternString for the RollingFileAppender <strong>in code</strong>?</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public class Log4NetInitializer
  {
      public static void Setup()
      {
          Hierarchy hierarchy = (Hierarchy) LogManager.GetRepository();
  
          PatternLayout patternLayout = new PatternLayout();
          patternLayout.ConversionPattern = "%date %-5level %logger [%ndc] %thread %newline%message%newline%exception";
          patternLayout.ActivateOptions();
  
          RollingFileAppender fileLogRoot = new RollingFileAppender();
          fileLogRoot.AppendToFile = true;
          fileLogRoot.File = @"C:\Log\MyLogFile.txt";
          fileLogRoot.Layout = patternLayout;
          fileLogRoot.MaxSizeRollBackups = 5;
          fileLogRoot.Encoding = Encoding.UTF8;
          fileLogRoot.LockingModel = new FileAppender.MinimalLock();
          fileLogRoot.ActivateOptions();
          hierarchy.Root.AddAppender(fileLogRoot);
  
          hierarchy.Root.Level = Level.Info;
          hierarchy.Configured = true;
      }
  }
  </code></pre>