无法解决 GAC 的 log4net 和项目的 log4net 之间的冲突

No way to resolve conflict between log4net from GAC and log4net from project

我正在使用 .NET Framework 4.6.2 和 log4net 2.0.8 开发 C# 库项目。

当我尝试编译项目时出现以下错误:

error CS0433: The type 'log4net.ILog' exists in 'c:\Windows\assembly\GAC_32\log4net.2.10.0__692fbea5521e1304\log4net.dll' and also in 'd:\Desarrollo\packages\log4net.2.0.8\lib\net45-full\log4net.dll'

GAC中好像有另一个版本的log4net

为什么要用GAC版本编译?我有另一个使用 log4net 的项目,只有在这个项目中我得到了这个错误。

log4net 的参考文献是 D:\Desarrollo\packages\log4net.2.0.8\lib\net45-full\log4net.dll。并将其设置为本地副本。

我安装了包,再次添加,还是一样的错误。

详细错误是这样的:

1>C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3243: No way to resolve conflict between "log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" and "log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304". Choosing "log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" arbitrarily.
1>D:\MyProject\Loggers\ActionLogHelper.cs(28,20,28,24): error CS0433: The type 'ILog' exists in both 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' and 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'
1>D:\MyProject\Loggers\ActionLogHelper.cs(30,26,30,30): error CS0433: The type 'ILog' exists in both 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' and 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'
1>D:\MyProject\Loggers\ExceptionLogHelper.cs(22,20,22,24): error CS0433: The type 'ILog' exists in both 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' and 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'
1>D:\MyProject\Loggers\ExceptionLogHelper.cs(24,29,24,33): error CS0433: The type 'ILog' exists in both 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' and 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

问题是项目文件中有两个对同一个 dll 的引用。我用Notepad++打开工程文件,发现里面有这个:

  <ItemGroup>
    <Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
      <HintPath>..\..\..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\..\..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
      <HintPath>..\..\..\packages\Ninject.3.2.2.0\lib\net45-full\Ninject.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Net.Http" />
    <Reference Include="log4net">
      <HintPath>..\..\..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
    </Reference>

我已经删除了最后一个,现在可以编译了。