Wednesday, January 27, 2010

log4net and relative filename gotchas

I ran into some trouble using log4net.

I had set up Console and RollingFileAppender loggers in my app.config file.

In my code, I had the following:

log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo("myexe.exe"));
_log = log4net.LogManager.GetLogger(typeof(MyProgram));


When I ran the application via a shortcut in the folder where the program was located, log4net fired up just fine and posted to console and to a file. When I tried to run it as a service, log4net didn't create a file.

I think System.IO.FileInfo seemed to be starting its relative path in my executable's folder when started from there, but starting its relative path somewhere else when I started it as a service.

Once I changed my code to look like this:

log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo(FULL_PATH_TO_FOLDER+"myexe.exe"));
_log = log4net.LogManager.GetLogger(typeof(MyProgram));

it found my config file and started correctly creating its logs.