近期使用了Common.Logging的ILog接口作日誌接口,同時利用其log4net適配器與log4net記錄器來記錄系統日誌,在使用過程當中碰到了log4net版本問題。html
項目組件管理中安裝組件包:工具
PM> Install-Package Common.Loggingpost
PM> Install-Package Common.Logging.Log4Net 【2.0.1版本;依賴Common.Logging (≥ 2.0.0); log4net (= 1.2.10)】spa
項目中將引用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」版本。code
將Common.Logging.Log4Net版本刪除,從新安裝如下版本xml
PM> Install-Package Common.Logging.Log4Net1211 【2.2.0版本;Common.Logging (≥ 2.0.0);Log4Net (≥ 1.2.11)】htm
項目中log4net.dll版本爲 1.2.11.0, 運行OK,升級log4net1.2.13版本,升級成功,運行OK;接口
注意配置文件中程序集文件名差別get
<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" />
</
dependentAssembly
>
</
assemblyBinding
>
</
runtime
>