While I agree with the Mauricio Scheffer answer in general, there may be times when a DLL needs to register before the main application. In my case, I have implemented a SOAP extension class in a DLL to register SOAP requests and responses for any ASMX web service. Because the soap extension is in the DLL, which is executed before the web method is executed, log4net must be programmed in the DLL programmatically. But each web service contains its own log4net.config file, which the DLL needs to find and load in order to program log4net programmatically.
My solution was to add a method to locate 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); }
source share