Log4Net and GAC - How to link to configuration files?

I use log4net during my development as part of a project constraint, now I need to add it to the global assembly cache.

Logging definitions are in the Log4Net.xml file. This file refers to my assembly info as: [assembly: log4net.Config.XmlConfigurator (ConfigFile = "Log4Net.xml", Watch = true)]. As long as the xml file is in the same directory as log4net.dll, everything works fine.

However, now that I have added log4net to the GAC, it no longer compiles the XML file.

Does anyone know what I need to change in order to pick up the XML file again? Is a hardcoding patch an assembly reference the only way?

Many thanks

0
gac log4net
source share
2 answers

You can make sure that your log4net.xml file is set to Always Copy (right-click on log4net.xml → Properties → Copy to Output Directory = Always Copy). To make sure your configuration file is copied, you should check the bin \ debug or bin \ release directory and make sure that the log4net.xml file exists in the same directory that your application runs.

If this does not work, you can try turning on internal debugging in log4net. To enable internal debugging, add the following key to the app.config file. This will send log4net internal debugging messages to the Visual Studio output window (View → Output).

<configuration> <appSettings> <add key="log4net.Internal.Debug" value="true"/> </appSettings> </configuration> 

For more information on log4net internal debugging, you can mark the Phil Haack blog post here .

If all else fails, you can enable internal debugging and explicitly load the configuration by calling the log4net XmlConfigurator.ConfigureAndWatch method.

 var fi = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml"); XmlConfigurator.ConfigureAndWatch(fi); 
0
source share

log4net expects the configuration file to be in the path returned:

  System.AppDomain.CurrentDomain.BaseDirectory 

Let your application print this information in some kind of file, and then you know where you need to place the configuration file.

Of course, there are other solutions, but you can no longer use this attribute. Calling the ConfigureAndWatch () method directly allows you to find out where the configuration file is; you can even determine the location (it doesn't have to be a hard-coded path).

+3
source share

All Articles