开发者

log4net dependency problem

开发者 https://www.devze.com 2022-12-27 05:43 出处:网络
I have an issue with log4net which has been bugging me for a while and I\'ve resolved to sort it. I have a class library which references log4net. If I reference this class library in another project

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

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

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开发者_如何学Go don't speak gibberish...

Cheers guys

Alex..


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

https://learn.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.

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


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


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.

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 :

[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.

I found this solution here.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消