log4net In DLL Project
log4net is very popular open source logging utility for .NET, there are a lot that have been written about it mainly on how to setup and how to configure, but still there could be some issues that you may find difficult to deal with. Issues like logs are not generated or how to use it in a dll project in order to ascertain the logging is enabled for dll with or without any control of master application.
First and for most: log4net configuration section must be loaded before log4net can start producing logs. Most of the time the frustration of seeing no logs is caused by this problem.
There are two ways of loading log4net configuration:
Method 1. Through .NET configuration framework, embed your log4net config section xml in system config files like web.config for ASP.NET or app.config for a windows app or dll, and do not forget to explicitly tell log4net to load the config like this:
Method 2. Through an independent log4net.config file, the file name does not matter as long as it matches the physical file, and same to run following to explicitly load the config file:
log4net.Config.XmlConfigurator. ConfigureAndWatch(new FileInfo( Path.GetDirectoryName( Assembly.GetAssembly(typeof(ThisClass)).Location) + @"\" + "mylog4net.config"));
A few points here that I would like your attention:
1. Use different XmlConfigurator methods depending where the config is, do not mess up one with another
2. In the second example, path of the config file must be given, if it is not presented, it will use the default directory which is “c:\”
3. I would highly recommend to go with the second option, as it works in a more distributed way, for example log4net in a dll project.
Now back to our topic: how to use log4net in a dll project.
It also depends on how the dll project is referenced by master application.
3. If the master application references the dll project through ‘projects’, all you need to do is to place a mylog4net.config in your dll project, and use method 2 to start log4net in your dll project (as early as possible)
As a result of build, mylog4net.config in dll directory will be copied to master project bin folder and gets loaded whenever dll is called, so even master project does not use log4net, your dll project will be still logged.
4. If the master application references the dll project through ‘browse’ which is using browsing and select dll only in visual studio in ‘add reference…’.
In this way, mylog4net.config in your dll project will not be copied to the master bin folder as a result of build, you will HAVE TO MANUALLY copy mylog4net.config file to the bin folder. Again in yout dll project you will need to use method 2 to load config.
Use my method 2 to load config file from the execution folder, and ensure that the log4net config file exists in the execution folder, this will guarantee the logs will be exported.
Updated on 11/09/2012
When log4net is wrapped in a dll, some of its internal variables will record values in wrapper dll rather than the caller’s dll, for example %file is the file of log4net logging event, not the wrapper event.
You can add caller’s information that you wanted to log into log4net contexts.
When you pass an exception to wrapper dll, you still can get the whole exception output in message.