【问题标题】:Exception in Common.Logging.dll: Failed obtaining configuration for Common.Logging from configuration section 'common/logging'Common.Logging.dll 中的异常:无法从配置部分“common/logging”获取 Common.Logging 的配置
【发布时间】:2023-03-10 11:16:01
【问题描述】:

在你想知道之前,我已经阅读了 Similar Topic 并尝试在 app.config 中的工厂适配器标签中重命名“Common.Logging.Log4Net”,但这对我没有帮助。我还尝试注释掉启动标签和运行时标签。

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

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

根据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 日志的初始化行中,消息如下:

Common.Logging.dll 中出现“Common.Logging.ConfigurationException”类型的未处理异常

附加信息:无法从配置部分“common/logging”获取 Common.Logging 的配置。

【问题讨论】:

  • 你的logger dll不在exe的路径中
  • 是的,它和.exe在同一个~/bin/debug目录下,有Common.Logging.dll和Common.Logging.Log4Net1213.dll和log4net.dll

标签: c# .net log4net common.logging


【解决方案1】:

你得到一个ConfigurationException,因为你的配置无效,特别是&lt;configSections&gt;元素的位置:

每个配置文件只允许一个&lt;configSections&gt; 元素,如果存在必须是根元素的第一个子元素

所以你的配置文件应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多