使用Common.Logging与log4net的组件版本兼容问题

8/3/2015来源:C#应用人气:2721

使用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>