C# 无法从配置部分“common/logging”获取 Common.Logging 的配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11368267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:36:29  来源:igfitidea点击:

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

c#log4netcommon.logging

提问by jdecuyper

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)
  • 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.

似乎无法解析我的配置文件,有没有人知道正确的格式应该是什么,还是有其他错误?我使用官方文档创建了我的配置文件。

采纳答案by Jeroen

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

您的应用程序有两个问题(我下载的那个):

  1. Your configSections in app.config looks like this:
  1. app.config 中的 configSections 如下所示:
<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.

注意到 log4net-section 被声明了两次吗?删除第一个。

  1. After removing the first log4net-section, I get the following:
  1. 删除第一个 log4net-section 后,我得到以下信息:

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)

无法加载文件或程序集“log4net,版本=1.2.11.0,文化=中性,PublicKeyToken=669e0ddf0bb1aa2a”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 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.

我从 log4net 网站下载了 log4net 1.2.11.0,将其解压缩,解锁了 dll 并替换了您示例中的 log4net,它似乎可以工作。

回答by sgmoore

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

您可以尝试使用 Common.Logging.dll 版本 2.1.1.0。

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.

您可以自己下载并比较这两个版本的源代码,但据我所知,2.1.0.0 和 2.1.1.0 之间的唯一区别是与读取配置设置相关的更改,以便解决 Framework 4.0 的错误。该错误的描述(请参阅http://support.microsoft.com/kb/2580188)是指从网络共享运行,而我不是从网络运行,但使用 2.1.0.0 的测试应用程序会产生与以下相同的错误你得到而 2.1.1.0 没有。

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.

如果您正在使用另一个需要 common.logging.dll 版本 2.1.0.0 的库,那么您应该能够使用程序集重定向来使用 2.1.1.0。

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

不确定它是否相关,但我将 dll 的名称保留为 Common.Logging.Log4Net121.dll 并修改了 app.config

回答by Isak Savo

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).

该异常与 OP 相同,并且它内部几级的内部异常未能找到Common.Logging.Log4Net( FileNotFoundException)。

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

似乎对于 Common.Logging.Log4Net1211 NuGet 包,他们已将程序集名称重命名为Common.Logging.Log4Net1211而不是简单的Common.Logging.Log4Net。这意味着app.config您需要引用这个新的程序集名称: <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">

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

这是我在 app.config 的整个 common/logging 部分以供参考:

<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>

回答by David Yates

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.

我发现这是由于 Common.Logging.Log4Net1211 NuGet 包检索了旧版本的 Common.Logging。检查 Common.Logging 的 NuGet 更新,如果找到更新,请下载并重试。

回答by Jaanus

I got it working by installing a missing package

我通过安装一个丢失的包让它工作

Install-Package Common.Logging.Log4Net1211

回答by Ostati

I'm using

我正在使用

Common.Logging v3.3.1.0

Common.Logging v3.3.1.0

with

Common.Logging.Log4Net1213 v3.3.1.0

Common.Logging.Log4Net1213 v3.3.1.0

in ASP.NET Web API v5, which throws exception

in ASP.NET Web API v5,抛出异常

"parent configuration sections are not allowed"

“不允许父配置节”

Exception is thrown from Common.Logging.ConfigurationSectionHandler.Createmethod

Common.Logging.ConfigurationSectionHandler.Create方法抛出异常

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

为了使这项工作,我必须在语法上配置适配器

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.

我仍然需要弄清楚为什么 IIS/Express 两次调用 Create 方法,这导致在 if 条件中抛出异常,但至少现在压力是关闭的。

回答by Maxim Eliseev

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

如果您的 log4net 是 2.0.6 或更高版本,使用 Common.Logging.Log4Net.Universal 包可能会很方便。

回答by an earwig

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 CAMPapplication and the APIapplication under it are both using Common.Logging. Because of this, both web.configfiles get loaded, Common.Logging reads CAMP's configuration, then it sees that APIhas configuration and tries to read that as well, sees the Common.Logging section and throws up because it already read that from the CAMPapplication.

请注意,在 IIS 中,我将 API 应用程序托管在另一个应用程序下。在这种情况下,CAMP应用程序及其下的API应用程序都使用 Common.Logging。因此,这两个web.config文件都被加载, Common.Logging 读取CAMP的配置,然后它看到API有配置并尝试读取它,看到 Common.Logging 部分并抛出,因为它已经从CAMP读取了应用。

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

最后,解决方案是将API从IIS 中的CAMP应用程序下移出。有点晦涩的边缘情况,但也许有一天其他人可能会面临这个问题。