While I agree with Mauricio Scheffer's answer in general, there may be cases where the DLL needs to log before the main application. In my case, I implemented a SOAP extension class in a DLL to log SOAP requests and responses for any ASMX web service. Because the soap extension is in a DLL which is executed before the web method execution, log4net must be configured programmatically in the DLL. But each web service contains its own log4net.config file which the DLL needs to locate and load to configure log4net programmatically.
My solution was to add a method to determine to location of the running DLL
static public string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
and then in the constructor of my DLL to load the configuration programmatically
if (!log4net.LogManager.GetRepository().Configured)
{
// assume that log4net.config is located in the root web service folder
var configFileDirectory = (new DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent;
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);
}