Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure log4net logging in dll

I'm setting up a dll to be used as a third party dll for a different application. I want this dll to have it's own logging so the external application doesn't have to deal with setting up anything (I don't believe they use the same logging as we do). I've read that may not be the best solution but it's the task I've been given. We want to use log4net with this. I've looked at a few of the other questions on here and they mention that it is configurable via code, however, the main issue I'm having is that there is no clear cut entry point into our code to configure log4net. I'm curious if I should just abandon having the dll configure itself and have a method that is called by the secondary application that configures the dll's logging or if there is a better way to go about this. Any input would be much appreciated

like image 547
Thomas Avatar asked Apr 02 '13 16:04

Thomas


1 Answers

You can configure log4net programmatically. Perhaps add this code to the constructor of your DLL.

if (!log4net.LogManager.GetRepository().Configured)
{
    // my DLL is referenced by web service applications to log SOAP requests before
    // execution is passed to the web method itself, so I load the log4net.config
    // file that resides in the web application root folder
    var configFileDirectory = (new DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent; // not the bin folder but up one level
    var configFile = new FileInfo(configFileDirectory.FullName + "\\log4net.config");

    if (!configFile.Exists)
    {
        throw new FileLoadException(String.Format("The configuration file {0} does not exist", configFile));
    }

    log4net.Config.XmlConfigurator.Configure(configFile);
}
like image 66
Tim Partridge Avatar answered Nov 05 '22 19:11

Tim Partridge