Search code examples
c#.netlog4netcommon.logging

Exception in Common.Logging.dll: Failed obtaining configuration for Common.Logging from configuration section 'common/logging'


Before you wonder, I've read the Similar Topic and tried renaming the 'Common.Logging.Log4Net' in factory adapter-Tag in app.config but this didn't help me. Also I tried commenting out the startup-Tag and runtime-Tag.

So I've downloaded the Common.Logging.LogNet1213.3.3.1 NuGet Package. Now in my project there are the packages

  • Common.Logging.3.3.1
  • Common.Logging.Core.3.3.1
  • Common.LoggingLog4Net1213.3.3.1
  • log4net.2.0.5

According to the Documentation I filled my app.config, which now looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.15.0" newVersion="1.2.15.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <configSections>

    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

  </configSections>

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <log4net>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </root>

    <logger name="MyApp.DataAccessLayer">
      <level value="DEBUG" />
    </logger>

  </log4net>
</configuration>

My C# Console App for trying Common.Logging looks like this:

using Common.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLogging
{
    class Program
    {
        static void Main(string[] args)
        {
            ILog log = LogManager.GetLogger<Program>();
            log.Debug("qwerty");
        }
    }
}

The exception appears in the initialisation line of ILog log and this is the message:

An unhandled exception of type 'Common.Logging.ConfigurationException' occurred in Common.Logging.dll

Additional information: Failed obtaining configuration for Common.Logging from configuration section 'common/logging'.


Solution

  • You're getting a ConfigurationException because your configuration is invalid, specifically the position of the <configSections> element:

    Only one <configSections> element allowed per config file and if present must be the first child of the root element

    So your config file should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>    
      <configSections>
    
        <sectionGroup name="common">
          <section name="logging" 
                   type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
    
        <section name="log4net" 
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    
      </configSections>
    
      <!-- everything else -->