Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to configure a console application with the following logging assemblies:

  • Common.Logging.dll (2.1.0.0)
  • Common.Logging.Log4Net1211.dll (2.1.0.0)
  • log4net.dll (1.2.11.0)

If the logger gets configured programmatically then everything works fine:

NameValueCollection properties = new NameValueCollection(); properties["showDateTime"] = "true";    
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);

But if I try to launch it using the following configuration file, it blows up:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
    </configSections>

    <common>
    <logging>
        <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
             <arg key="configType" value="FILE-WATCH"/>
            <arg key="configFile" value="~/Log4NET.xml"/>
        </factoryAdapter>
    </logging>
</common>
</configuration>

These are the relevant error messages:

{"Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'."}

{"Failed obtaining configuration for Common.Logging from configuration section 'common/logging'."}

It seems to being unable to parse my configuration file, does anyone know what the correct format should be or is it something else that's wrong? I created my configuration file using the official documentation.

like image 291
jdecuyper Avatar asked Jul 06 '12 19:07

jdecuyper


2 Answers

I was having this (or related) issue as well and after half a day of error hunting and debugging I narrowed it down to a configuration problem.

The exception was the same as the OP and the inner exception a few level inside of it was failing to find Common.Logging.Log4Net (FileNotFoundException).

It seems that for the Common.Logging.Log4Net1211 NuGet package, they have renamed the assemblyname to be Common.Logging.Log4Net1211 instead of simply Common.Logging.Log4Net. This means in your app.config you need to refer to this new assembly name: <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">

Here's my entire common/logging section of app.config for reference:

<common>
  <logging>
    <!-- Notice that it's Log4net1211 -->
    <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">
      <arg key="configType" value="FILE-WATCH" />
      <arg key="configFile" value="~/Log4Net-MAIN.config" />
    </factoryAdapter>
  </logging>
</common>
like image 89
Isak Savo Avatar answered Nov 02 '22 16:11

Isak Savo


There are two problems with your application (the one I downloaded):

  1. Your configSections in app.config looks like this:
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

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

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

Notice that the log4net-section is declared twice? Remove the first one.

  1. After removing the first log4net-section, I get the following:

Could not load file or assembly 'log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I downloaded log4net 1.2.11.0 from the log4net website, unzipped it, unblocked the dll and replaced the log4net in your example and it seems to work.

like image 24
Jeroen Avatar answered Nov 02 '22 16:11

Jeroen