title: How to get Log4Net to use app.config
body: <p>The Story:  I have a WinForms application with multiple assemblies - MainApp and Utilities.  I use Utilities for my logging via log4net.  All is fine with the world <em>if</em> I use a separate config file for log4net (aka log4net.config) - no issues.</p>
  
  <p>However, IT staff finds it challenging to tweak two .config files (MainApp.exe.config and log4net.config).  So, I'm trying to add my log4net config settings into my app.config file.  Not having any luck.</p>
  
  <p>I've tried several solutions posted in this forum.  This one seemed to experience the same error I get:
  <a href="https://stackoverflow.com/questions/17468841/log4net-configuration-failed-to-find-section">log4net configuration - failed to find section</a></p>
  
  <p>I've tried putting this line in my Utilities:AssemblyInfo.cs</p>
  
  <pre><code>[assembly: log4net.Config.XmlConfigurator(ConfigFile = "MainApp.exe.config", Watch = false)]
  </code></pre>
  
  <p>In my Utilities module where log4net is referenced, I have this:</p>
  
  <pre><code>const string TEMP_VARIABLE = "TEMP";
  string tempDir = Environment.GetEnvironmentVariable( TEMP_VARIABLE );
  StringBuilder userTempDirLogFile = new StringBuilder( tempDir );
  userTempDirLogFile.Append( @"\" );
  userTempDirLogFile.Append( Environment.MachineName );
  userTempDirLogFile.Append( "_MAIN_" );
  userTempDirLogFile.Append( DateTime.Now.DayOfWeek );
  userTempDirLogFile.Append( ".log" );
  log4net.GlobalContext.Properties["MainLogFileName"] = userTempDirLogFile.ToString();
  StringBuilder utilityLogFile = ( userTempDirLogFile.Replace( "_MAIN_", "_UTILITIES_" ) );
  log4net.GlobalContext.Properties["UtilityLogFileName"] = utilityLogFile.ToString();
  log4net.Config.XmlConfigurator.Configure();
  _mainLogger = LogManager.GetLogger( "MAIN" );
  </code></pre>
  
  <p>_mainLogger gets this error message:</p>
  
  <pre><code>log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the &lt;log4net&gt; and &lt;configSections&gt; elements. The configuration section should look like: &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /&gt;
  </code></pre>
  
  <p>I noticed in log4net source code that log4net.Config.XmlConfigurator.Configure() calls Assembly.GetCallingAssembly().  I've verified that GetCallingAssembly() is indeed MainApp.  My program directory contains all necessary files.</p>
  
  <p>This is my app.config</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
      &lt;!-- configSections MUST be first! --&gt;
      &lt;configSections&gt;
          &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /&gt;
      &lt;/configSections&gt;
  
      &lt;log4net&gt;
          &lt;appender name="MainLogFile" type="log4net.Appender.RollingFileAppender"&gt;
              &lt;file type="log4net.Util.PatternString" value="%property{MainLogFileName}"/&gt;
              &lt;appendToFile value="false" /&gt;
              &lt;maximumFileSize value="20MB" /&gt;
              &lt;maxSizeRollBackups value="3" /&gt;
              &lt;param name="Encoding" value="unicodeFFFE" /&gt;
              &lt;layout type="log4net.Layout.PatternLayout"&gt;
                  &lt;conversionPattern value="%date{ISO8601}&amp;#9;%property{messageId}&amp;#9;%-5level&amp;#9;%message%newline" /&gt;
              &lt;/layout&gt;
              &lt;filter type="log4net.Filter.LevelRangeFilter"&gt;
                  &lt;param name="LevelMin" value="ALL" /&gt;
                  &lt;param name="LevelMax" value="OFF" /&gt;
              &lt;/filter&gt;
          &lt;/appender&gt;
  
          &lt;appender name="UtilityLogFile" type="log4net.Appender.RollingFileAppender"&gt;
              &lt;file type="log4net.Util.PatternString" value="%property{UtilityLogFileName}"/&gt;
              &lt;appendToFile value="false" /&gt;
              &lt;maximumFileSize value="20MB" /&gt;
              &lt;maxSizeRollBackups value="3" /&gt;
              &lt;param name="Encoding" value="unicodeFFFE" /&gt;
              &lt;layout type="log4net.Layout.PatternLayout"&gt;
                  &lt;conversionPattern value="%date{ISO8601}&amp;#9;%property{messageId}&amp;#9;%-5level&amp;#9;%message%newline" /&gt;
              &lt;/layout&gt;
              &lt;filter type="log4net.Filter.LevelRangeFilter"&gt;
                  &lt;param name="LevelMin" value="ALL" /&gt;
                  &lt;param name="LevelMax" value="OFF" /&gt;
              &lt;/filter&gt;
          &lt;/appender&gt;
  
          &lt;logger name="MAIN"&gt;
              &lt;level value="DEBUG" /&gt;
              &lt;appender-ref ref="MainLogFile" /&gt;
          &lt;/logger&gt;
  
          &lt;logger name="UTILITY"&gt;
              &lt;level value="DEBUG" /&gt;
              &lt;appender-ref ref="UtilityLogFile" /&gt;
          &lt;/logger&gt;
      &lt;/log4net&gt;
  
      &lt;startup&gt;
          &lt;!-- Leave sku out so that both 4.0 and 4.5 are supported --&gt;
          &lt;supportedRuntime version="v4.0" /&gt;
      &lt;/startup&gt;
  
      &lt;system.windows.forms jitDebugging="true" /&gt;
  
      &lt;system.diagnostics&gt;
          &lt;trace useGlobalLock="false" /&gt;
      &lt;/system.diagnostics&gt;
  
      &lt;appSettings&gt;
          &lt;add key="PRINT_CALL_STACK" value="false"/&gt;
          &lt;add key="TRACK_PERFORMANCE" value="false"/&gt;
          &lt;add key="USING_TEST_MODE" value="false"/&gt;
          &lt;add key="WAIT_FOR_LOGON" value="0"/&gt;
      &lt;/appSettings&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I figure I'm missing something, but no clue as to what.  Thanks for your time.</p>
  
  <p>Note: using VS2013 and log4net 1.2.13.0. Solution targets .NET 4.0 full and x86.</p>