如何成功配置Common.Logging?

时间:2013-02-12 19:41:24

标签: asp.net-mvc nlog common.logging

我正在尝试使用NuGet包来配置Common.Logging以在ASP.Net MVC项目中使用NLog2。根据以下网址提供的信息,我相信我的记录器配置正确,但我仍然遇到配置错误。

Common.Logging Configuration Instructions

NLog Configuration Tutorial

我已按照以下说明将以下内容添加到 web.config

<configuration>
  <configSections>
      <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
      <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
      </sectionGroup>
    </configSections>
    <common>
      <logging>
        <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
      </logging>
    </common>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <targets>
        <target name="logfile" xsi:type="file" filename="e:\logfile.txt" layout="${date:format=yyyy/MM/dd HH:mm:ss} ${message}" />
      </targets>
      <rules>
        <logger name="*" minlevel="Trace" writeTo="logfile" />
      </rules>
    </nlog> 
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
</configuration>

据我所知,这是我需要做的一切,但是当我尝试运行Web项目时,我遇到配置错误......

  

分析程序错误消息:为common / logging创建配置节处理程序时出错:无法创建类型'Common.Logging.NLog.NLogLoggerFactoryAdapter,Common.Logging.NLog20'

有人可以提供有关缺失或错误的建议吗?

2 个答案:

答案 0 :(得分:13)

问题似乎是Common.Logging NuGet包(v2.0.0)添加的默认配置不正确。

web.config中的运行时部分需要更改为以下内容:

<dependentAssembly>
   <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="2.0.0.0" />
  </dependentAssembly>

请注意oldVersion值。这似乎是造成错误的原因(至少基于我在上述问题中概述的情景)。

另请参阅此相关GitHub问题:Possible Issues with Common.Logging / Common.Logging.NLog20 NuGet packages

答案 1 :(得分:1)

使用

为我工作
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog2">

而不是

<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">

结束时注意20 vs 2。