visual-studio log4net依赖问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2629989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:45:12  来源:igfitidea点击:

log4net dependency problem

c#.netvisual-studiolog4net

提问by Fergal Moran

I have an issue with log4net which has been bugging me for a while and I've resolved to sort it.

我有一个 log4net 问题,它一直困扰着我一段时间,我已决定对其进行排序。

I have a class library which references log4net. If I reference this class library in another project I must then reference log4net in this project otherwise I get a build error

我有一个引用 log4net 的类库。如果我在另一个项目中引用这个类库,那么我必须在这个项目中引用 log4net 否则我会收到构建错误

Unknown build error, 'Cannot resolve dependency to assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' 

I'm aware that the error message is probably telling me the solution, unfortunately I don't speak gibberish...

我知道错误消息可能告诉我解决方案,不幸的是我不会说胡言乱语......

Cheers guys

干杯伙计们

Alex..

亚历克斯..

采纳答案by kemiller2002

Here is the link which describes what is happening and how to fix it:

这是描述正在发生的事情以及如何解决它的链接:

https://docs.microsoft.com/en-us/archive/blogs/jmstall/viewing-types-with-reflection-only

https://docs.microsoft.com/en-us/archive/blogs/jmstall/viewing-types-with-reflection-only

An excerpt from the above link:

摘自上述链接:

So what happened was that it tried to get the System.Type for Bar, but to resolve the type it needs to load the base class, which is in another dll. Reflection-Only context doesn't do binding policy so it can't find that dll. The LoaderException hint says to use the ReflectionOnlyAssemblyResolve, which provides more information about this.

所以发生的事情是它试图获取 Bar 的 System.Type,但要解析它需要加载基类的类型,基类位于另一个 dll 中。仅反射上下文不执行绑定策略,因此无法找到该 dll。LoaderException 提示说要使用 ReflectionOnlyAssemblyResolve,它提供了有关此的更多信息。

To use the reflection API, you have to resolve all the dependencies used.

要使用反射 API,您必须解析所有使用的依赖项。

回答by Geo242

I had the same problem with log4net when creating a new WPF project and adding a reference to another project that referenced log4net. I resolved the issue by adding the log4net.dll to the GAC using these instructions: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx

在创建新的 WPF 项目并添加对另一个引用 log4net 的项目的引用时,我遇到了与 log4net 相同的问题。我通过使用以下说明将 log4net.dll 添加到 GAC 解决了该问题:http: //msdn.microsoft.com/en-us/library/dkkx7f79.aspx

回答by Ruben Van Den Abeele

I know it's been a while but I wanted to share what solved it for me.

我知道已经有一段时间了,但我想分享为我解决的问题。

It looks like the main cause for this problem is the way log4net requires it's config file in the assembly info of the class library. WPF doesn't seem to like that. Referencing log4net in the wpf app itself caused the log4net config file to be overwritten for me, as the wpf app is later in the build order and generates a default log4net config file.

看起来这个问题的主要原因是 log4net 在类库的程序集信息中要求它的配置文件的方式。WPF 似乎不喜欢那样。在 wpf 应用程序本身中引用 log4net 会导致 log4net 配置文件被我覆盖,因为 wpf 应用程序在构建顺序的后面并生成默认的 log4net 配置文件。

So to fix your class library for use with assembly preloading, edit it as follows:

因此,要修复您的类库以用于程序集预加载,请按如下方式对其进行编辑:

In the class library delete this line in AssemblyInfo.cs :

在类库中删除 AssemblyInfo.cs 中的这一行:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

Now in the entry point of your library setup your logger like this:

现在在您的库的入口点设置您的记录器,如下所示:

FileInfo configFileInfo = new FileInfo("log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo);

If you setup log4net in the app.config file, just reference that one.

如果您在 app.config 文件中设置 log4net,只需引用该文件即可。

I found this solution here.

我在这里找到了这个解决方案。

回答by BuilderBee

I had the same problem. I still don't fully understand it, but I can tell you how I solved my problem. I had a unit test project B with a project reference to project A, which references log4net. So to me the gibberish means when Visual Studio is trying to create the .accessor file for the unit test project, it reflects on project A. This means it tries to load project A's references, but the assembly loader can't find it because I don't have log4net in the GAC, just locally for project A to reference. In my case, adding log4net to my DEVPATH (GAC would work, too) was the solution.

我有同样的问题。我仍然不完全理解它,但我可以告诉你我是如何解决我的问题的。我有一个单元测试项目 B,其中项目引用了项目 A,该项目引用了 log4net。所以对我来说,胡言乱语意味着当 Visual Studio 尝试为单元测试项目创建 .accessor 文件时,它反映在项目 A 上。这意味着它尝试加载项目 A 的引用,但程序集加载器找不到它,因为我GAC中没有log4net,只是在本地供项目A参考。就我而言,将 log4net 添加到我的 DEVPATH(GAC 也可以)是解决方案。