Common.Logging.dll 中的异常:无法从配置部分 'common/logging' 获取 Common.Logging 的配置

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

在您想知道之前,我已经阅读了 Similar Topic 并尝试在 app.config 中重命名工厂适配器标签中的 'Common.Logging.Log4Net' 但这对我没有帮助。我还尝试注释掉 startup-Tag 和 runtime-Tag。

所以我下载了 Common.Logging.LogNet1213.3.3.1 NuGet 包。现在在我的项目中有包

根据 Documentation 我填写了我的 app.config,现在看起来像这样:

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

我尝试 Common.Logging 的 C# 控制台应用程序如下所示:

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");
        }
    }
}

异常出现在 ILog 日志的初始化行,这是消息:

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

您收到 ConfigurationException 因为您的配置无效,特别是 <configSections> 元素的位置:

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

因此您的配置文件应如下所示:

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