user7014262
user7014262

Reputation: 135

log4net xmlconfigurator.configure needs to use the dll embedded xml file

I am creating a console application (exe) and trying to log the errors using log4net. If I give any of the below commands in the main method the logger works fine:

XmlConfigurator.Configure(new FileInfo(@"filepath\log4net.config")); 

or

using (FileStream fs = new FileStream("..\\..\\log4net.config",FileMode.Open))
              { XmlConfigurator.Configure(fs); }

But I need to have the log4net.config file separately. So my question is - how do I refer to the log4net.config file (which is embedded in the exe) in my main method to enable the logger, so that I don't need to have the separate log4net config file other than the exe file? It makes easier for moving exe alone to other machine.

Note: log4net.config file is an embedded resource in the exe.

Upvotes: 2

Views: 1933

Answers (1)

stuartd
stuartd

Reputation: 73253

To load the config from an embedded file, the best way is to use the overload of Configure that takes a stream, for example:

 var assembly = Assembly.GetExecutingAssembly(); // or GetEntryAssembly()
 var resourceName = "log4net.config";

 using (Stream stream = assembly.GetManifestResourceStream(resourceName))
 {
        XmlConfigurator.Configure(stream);
 }

Upvotes: 4

Related Questions