title: How do I stop TopShelf creating multiple log files using log4net?
body: <p>One of my TopShelf-hosted Windows services is creating duplicate log files that look like this:</p>
  
  <pre><code>myapp.20140729.log
  myapp.20140729.log.20140729.log
  </code></pre>
  
  <p>A similar problem has been described on StackOverflow before - the solutions in <a href="https://stackoverflow.com/questions/10639682/log4net-rollingfileappender-generates-duplicate-logs">10639682</a> didn't work for me, but <a href="https://stackoverflow.com/questions/579688/why-is-the-date-appended-twice-on-filenames-when-using-log4net">579688</a> suggests it could be caused by initialising the logging system twice.</p>
  
  <p>I'm using the TopShelf Log4Net plugin, but I'm also doing my own log4net logging and need access to the <code>ILog</code> instance before the service has actually started, so my code looks like this:</p>
  
  <pre><code>public static void Main(string[] args) {
      XmlConfigurator.Configure();
      var log = LogManager.GetLogger(typeof(MyService));
      var container = new Container();
      RegisterComponents(container, log);
      log.InfoFormat("Starting MyService");
      RunService(container);
  }
  
  public static void RunService(Container container) {
      HostFactory.Run(x =&gt; {
          x.Service&lt;PortalAdaptor&gt;(s =&gt; {
              s.ConstructUsing(name =&gt; container.Resolve&lt;MyService&gt;());
              s.WhenStarted(f =&gt; f.Start());
              s.WhenStopped(f =&gt; f.Stop());
          });
          x.RunAsLocalService();
          x.SetDescription("My Service");
          x.SetDisplayName("My Service");
          x.SetServiceName("MyService");
          x.UseLog4Net();
      });
  }
  </code></pre>
  
  <p>I've done a little digging, and it appears that calling <code>x.UseLog4Net()</code> in my <code>RunService</code> method is actually running <code>XmlConfigurator.Configure()</code> <strong>again</strong> - see <a href="https://github.com/Topshelf/Topshelf/blob/32c92aefc3426085f52c3f756eaf57ecab06b1be/src/Topshelf.Log4Net/Logging/Log4NetLogWriterFactory.cs#L62" rel="nofollow noreferrer">Log4NetLogWriterFactory, line 62</a> in the TopShelf code.</p>
  
  <p>So...</p>
  
  <ol>
  <li>Am I correct in thinking that calling <code>XmlConfigurator.Configure()</code> twice could be the cause of my duplicate log file issues?</li>
  <li>If so - how can I either inject an existing config into TopShelf, OR get the TopShelf initialization to run before I start my service so I can start logging before the service starts?</li>
  </ol>