使用Common.Logging與log4net的組件版本兼容問題


近期使用了Common.Logging的ILog接口做日志接口,同時利用其log4net適配器與log4net記錄器來記錄系統日志,在使用過程中碰到了log4net版本問題。

項目組件管理中安裝組件包:

PM> Install-Package Common.Logging

PM> Install-Package Common.Logging.Log4Net  【2.0.1版本;依賴Common.Logging (≥ 2.0.0); log4net (= 1.2.10)】

項目中將引用log4net.dll版本為 1.2.10.0,在此配套版本情況下,運行出現錯誤: {"Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net'"}

也將log4net組件升級到最新版本1.2.13失敗,Install-Package : 將“log4net 1.2.10”更新到“log4net 2.0.3”失敗。找不到與“log4net 2.0.3”兼容的“Common.Logging.Log4Net”版本。

 

將Common.Logging.Log4Net版本刪除,重新安裝以下版本

PM> Install-Package Common.Logging.Log4Net1211  【2.2.0版本;Common.Logging (≥ 2.0.0);Log4Net (≥ 1.2.11)】

項目中log4net.dll版本為 1.2.11.0, 運行OK,升級log4net1.2.13版本,升級成功,運行OK;

注意配置文件中程序集文件名差異

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">

版本重定向

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />     
      </dependentAssembly>
  
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" />
      </dependentAssembly>
    </assemblyBinding>


原因主要是由於log4net version 1.2.11與1.2.10的publicKeyToken不同所致。可利用VS工具SN –T 組件文件名.dll查看公鑰。

log4net (≥ 1.2.11)  公鑰標記為 669e0ddf0bb1aa2a

log4net (= 1.2.10)  公鑰標記為 1b44e1d426115821

 

http://www.nuget.org/packages/Log4Net/

http://www.nuget.org/packages/Common.Logging/

http://www.nuget.org/packages/Common.Logging.Log4Net1211/

http://www.nuget.org/packages/Common.Logging.Log4Net/

http://developer.3l.nl/post/16463925249/log4net-1-2-11-dependency-hell

 

解決方式2,不同版本的publicKeyToken

< runtime >
     < assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       < dependentAssembly >
         < assemblyIdentity name="log4net" publicKeyToken="681549d62126b7b8" />
         < codeBase version="1.2.9.0" href="bin/log4net1.2.9.0/log4net1.2.9.0.dll" />
       </ dependentAssembly >
       < dependentAssembly >
         < assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
           < codeBase version="1.2.10.0" href="bin/log4net1.2.10.0/log4net1.2.10.0.dll" />
       </ dependentAssembly >
       < dependentAssembly >
         < assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
              <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" />
       </ dependentAssembly >
     </ assemblyBinding >
   </ runtime >


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM