Asp.net-Mvc

如何成功配置 Common.Logging?

  • April 5, 2014

我正在嘗試使用 NuGet 包將 Common.Logging 配置為在 ASP.Net MVC 項目中使用 NLog2。根據以下 URL 提供的資訊,我相信我的記錄器配置正確,但我仍然遇到配置錯誤。

Common.Logging 配置說明

NLog 配置教程

我已按照說明在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”

任何人都可以就缺少或錯誤的內容提供建議嗎?

問題似乎是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 問題:Common.Logging / Common.Logging.NLog20 NuGet packages 的可能問題

引用自:https://stackoverflow.com/questions/14840445