0

在您想知道之前,我已经阅读了 类似主题 并尝试在 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

根据文档,我填写了我的 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 的配置。

4

1 回答 1

0

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

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

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

<?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 -->
于 2016-10-19T11:28:35.357 回答