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

8

24

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.

Shook answered 6/7, 2012 at 19:7 Comment(2)
I believe the log4net 1.2.11 adapter library is Common.Logging.Log4Net1211, at least the nuget package that works is labelled so. I don't know what Common.Logging.Log4Net.dll is.Ouse
I'm using Common.Logging.Log4Net1211 renamed as Common.Logging.Log4Net.dll. I thought renaming could be the issue but it is not, same error is thrown. Thanks.Shook
A
14

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.

Aventurine answered 25/7, 2012 at 12:50 Comment(1)
Thanks a lot Jeroen, that did the trick. I was using the log4Net source to compile the DLL but for some reason it wouldn't work (the signing maybe?). Then I downloaded the new key binary instead, replaced the DLL and finally got this log file to appear!Shook
A
24

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>
Amritsar answered 22/8, 2012 at 11:3 Comment(3)
I was also having this same problem after updating from log4net 1.2.10 to 1.2.11 (a.k.a. 2.0.0 in NuGet). Thank you for your very helpful answer!Firelock
The same for log4net 1.2.13, of course adjusting the assembly name to reflect the new version. Thank you so much, this saved me hours of uttering profanities.Saguaro
For more information on this change, read here: github.com/net-commons/common-logging/wiki/…Olav
A
14

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.

Aventurine answered 25/7, 2012 at 12:50 Comment(1)
Thanks a lot Jeroen, that did the trick. I was using the log4Net source to compile the DLL but for some reason it wouldn't work (the signing maybe?). Then I downloaded the new key binary instead, replaced the DLL and finally got this log file to appear!Shook
M
3

I'm using

Common.Logging v3.3.1.0

with

Common.Logging.Log4Net1213 v3.3.1.0

in ASP.NET Web API v5, which throws exception

"parent configuration sections are not allowed"

Exception is thrown from Common.Logging.ConfigurationSectionHandler.Create method.

In order to make this work, I had to configure the adapter programmatically:

var properties = new Common.Logging.Configuration.NameValueCollection();
properties["configType"] = "INLINE";
Common.Logging.LogManager.Adapter = new Log4NetLoggerFactoryAdapter(properties);

I still have to figure out why IIS/Express calls the Create method twice, which is causing the exception to be thrown inside the if condition, but at least the pressure is off for now.

Marxism answered 20/9, 2016 at 16:0 Comment(1)
Having this same issue, I stepped in to the source and see Create get called twice. Did you ever learn anything further?Marlanamarlane
M
3

Although this is an old question, I had this issue a few weeks and none of the current answers seemed to remedy it. It seemed my configuration was correct as I had many other applications using near identical configuration with no issue. After quite a bit of debugging and running through stacktraces, I finally found the issue.

IIS

Notice that in IIS, I have the API application hosted under another application. In this case both the CAMP application and the API application under it are both using Common.Logging. Because of this, both web.config files get loaded, Common.Logging reads CAMP's configuration, then it sees that API has configuration and tries to read that as well, sees the Common.Logging section and throws up because it already read that from the CAMP application.

In the end the solution was to move the API out from under the CAMP application in IIS. A bit of an obsucre edge case, but perhaps someone else might face this issue some day.

Marlanamarlane answered 6/6, 2018 at 13:8 Comment(1)
OMG, I love you so much! It took me about 5h of my life to find this out! I had this issue on the IIS express and IIS standard... Bless you!Latifundium
I
2

I got it working by installing a missing package

Install-Package Common.Logging.Log4Net1211
Ichthyology answered 1/10, 2014 at 10:42 Comment(0)
S
1

Can you try with Common.Logging.dll version 2.1.1.0 instead.

You can download and compare the source of the two versions yourself, but as far as I can see the only difference between 2.1.0.0 and 2.1.1.0 is a change relating to reading the configuration settings in order to workaround a Framework 4.0 bug. The description of the bug (see http://support.microsoft.com/kb/2580188) refers to running from a network share which I am not running from a network, yet a test app using 2.1.0.0 generates the same error as you are getting whereas 2.1.1.0 doesn't.

If you are using another library that expects version 2.1.0.0 of common.logging.dll, then you should be able to using an assembly redirect to use 2.1.1.0 instead.

PS

Not sure whether it is relevant, but I left the name of the dll as Common.Logging.Log4Net121.dll and modified the app.config instead

Soni answered 19/7, 2012 at 16:26 Comment(6)
Thanks sgmoore! I'm working on this to see if it works, will soon get back to you.Shook
I did the test but with the same result, I uploaded a simple C# console project, it would be great if you could have a look at it: tempfiles.net/download/201207/254070/CommonLog4NetDemo.html Thanks again!Shook
Your app.config is referring to Common.Logging.Log4Net when the dll is called Common.Logging.Log4Net1211.dll so you need to change the app.config (line 14) to be <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211"> You also need to add a reference to log4net.dll.Soni
Thanks, I edited the app.config, added the reference to log4net 1.2.11.0 and uploaded the project to: tempfiles.net/download/201207/254264/CommonLog4NetDemo.html Same error is still thrown, I hope you still have some time to look at it.Shook
If I configure a simple console out logger adapter, it works fine though. But I need to add those log to an external file, maybe configure NLog is easier.Shook
I have amended your project and uploaded it to goo.gl/xaj0l . There were a few issues. Firstly your app.config had duplicate sections for log4net. Secondly the version of log4net.dll was wrong. Not sure where your version came from but it isn't signed with the PublicKeyToken that Common.Logging.Log4net1211 is expecting. The version which I got from NuGet works. I also changed your log.Debug to log.Info as your configuration file is currently ignoring debug messages (This was just to prove that it was logging both to the console and the file)Soni
I
1

I found that it was due to the Common.Logging.Log4Net1211 NuGet package retrieveing an older version of Common.Logging. Check for NuGet updates to Common.Logging and if you find one download it and try again.

Ingles answered 31/1, 2013 at 17:7 Comment(0)
K
0

If your log4net is 2.0.6 or above, it may be convenient to use Common.Logging.Log4Net.Universal package.

Koziel answered 17/10, 2017 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.