title: log4net configuration for project and unit test in one solution
body: <p>My solution contains a Windows Service application project and a Unit Test project, as shown here:</p>
  
  <p><a href="https://i.stack.imgur.com/DobXk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DobXk.jpg" alt="enter image description here"></a> </p>
  
  <hr>
  
  <h2>-------------------Windows Service application project Setup--------------------</h2>
  
  <hr>
  
  <p><strong>I've setup log4net in my Windows Service application project, as shown below:</strong></p>
  
  <p><strong>Step 1)</strong>  I've added the reference to log4net to my Windows Service application project.</p>
  
  <p><strong>Step 2)</strong>  My app.config looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
      &lt;/startup&gt;
  
      &lt;configSections&gt;
        &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/&gt;
      &lt;/configSections&gt;
  
      &lt;log4net&gt;
        &lt;appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"&gt;
          &lt;file value="log.log" /&gt;
          &lt;appendToFile value="true" /&gt;
          &lt;rollingStyle value="Size" /&gt;
          &lt;maxSizeRollBackups value="5" /&gt;
          &lt;maximumFileSize value="100KB" /&gt;
          &lt;staticLogFileName value="true" /&gt;
          &lt;layout type="log4net.Layout.PatternLayout"&gt;
            &lt;conversionPattern value="%date %level %logger - %message %exception%newline" /&gt;
          &lt;/layout&gt;
        &lt;/appender&gt;
        &lt;root&gt;
          &lt;level value="ALL" /&gt;
          &lt;appender-ref ref="LogFileAppender" /&gt;
        &lt;/root&gt;
      &lt;/log4net&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>Step 3)</strong>  I've added this the Program.cs main:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.Threading.Tasks;
  
  [assembly: log4net.Config.XmlConfigurator(Watch = true)]
  
  namespace MyAppService
  {
      static class Program
      {
          /// &lt;summary&gt;
          /// The main entry point for the application.
          /// &lt;/summary&gt;
          static void Main()
          {
              log4net.Config.XmlConfigurator.Configure();
  </code></pre>
  
  <p><strong>Step 4)</strong>  In my classes where I am logging I've added this:</p>
  
  <pre><code>private static ILog logger = LogManager.GetLogger(typeof(Reader));
  </code></pre>
  
  <p><strong>Step 5)</strong>  I've added logging statements like this:</p>
  
  <pre><code>logger.Info("Data Read Completed Successfully.");
  </code></pre>
  
  <hr>
  
  <h2>-----------------------------Unit Test project Setup----------------------------</h2>
  
  <hr>
  
  <p><strong>I've setup log4net in my Unit Test project, as shown here:</strong></p>
  
  <p><strong>Step 1)</strong>  I've added the reference to log4net to my Unit Test project.</p>
  
  <p><strong>Step 2)</strong>  I've added a app.config file that looks like this:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;
    &lt;appSettings&gt;
      &lt;add key="log4net.Internal.Debug" value="true"/&gt;
    &lt;/appSettings&gt;
    &lt;configSections&gt;
      &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/&gt;
    &lt;/configSections&gt;
    &lt;log4net&gt;
      &lt;!-- A1 is set to be a ConsoleAppender --&gt;
      &lt;appender name="A1" type="log4net.Appender.FileAppender"&gt;
        &lt;file value="logfile.txt" /&gt;
        &lt;appendToFile value="false" /&gt;
  
        &lt;!-- A1 uses PatternLayout --&gt;
        &lt;layout type="log4net.Layout.PatternLayout"&gt;
          &lt;conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" /&gt;
        &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;!-- Set root logger level to DEBUG and its only appender to A1 --&gt;
      &lt;root&gt;
        &lt;level value="DEBUG" /&gt;
        &lt;appender-ref ref="A1" /&gt;
      &lt;/root&gt;
    &lt;/log4net&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>Step 3)</strong>  I've added this the ForecastIOTest.cs unit test class:</p>
  
  <pre><code>using log4net;
  using log4net.Config;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using System;
  using System.Collections.Generic;
  using WeatherAppService;
  
  [assembly: log4net.Config.XmlConfigurator(Watch = true)]
  
  namespace WeatherAppTests
  {
      [TestClass]
      public class ForecastIOTest
      {
          private static ILog logger = LogManager.GetLogger(typeof(WeatherController));
  
          [AssemblyInitialize]
          public static void Configure(TestContext tc)
          {
              log4net.Config.XmlConfigurator.Configure();
          }
  </code></pre>
  
  <p><strong>Step 4)</strong>  In my test class where I am logging I've added this:</p>
  
  <pre><code>private static ILog logger = LogManager.GetLogger(typeof(ForecastIOTest));
  </code></pre>
  
  <p><strong>Step 5)</strong>  I've added logging statements like this:</p>
  
  <pre><code>logger.Info("This is a test.");
  </code></pre>
  
  <hr>
  
  <h2>-------------------------------------Problem------------------------------------</h2>
  
  <hr>
  
  <p><strong>Now, the error that I receive indicates that I have more than one configSections element:  One in the Service app and another in the test app.</strong></p>
  
  <pre><code>log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
  System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---&gt; System.Configuration.ConfigurationErrorsException: Only one &lt;configSections&gt; element allowed per config file and if present must be the first child of the root &lt;configuration&gt; element. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 9)
  </code></pre>
  
  <p><strong>But if I remove the the configSections from the test app, I receive this error:</strong></p>
  
  <pre><code>log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
  System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---&gt; System.Configuration.ConfigurationErrorsException: Unrecognized configuration section log4net. (C:\Users\pdl\Documents\Visual Studio 2013\Projects\WeatherAppService\WeatherAppTests\bin\Debug\WeatherAppTests.dll.config line 7)
  </code></pre>
  
  <hr>
  
  <h2>-------------------------------------Question------------------------------------</h2>
  
  <hr>
  
  <p>Does anybody know how to set up log4net, so I can run the unit test, which runs the service app and get my log files written to?  Or, given everything I have done, can someone please tell me what is incorrect or incomplete?</p>