Monday, 17 March 2008

Log4net - problems logging from a web app

There have been a couple of occasions when logging just won't work for me in a web application. This typically means that log4net is getting permission issues. The web application runs as a special user account on the web server called ASPNET (usually). This account has restricted permissions to protect the web server from attacks. By default this account may not have permission to write to the file system. Make sure that the ASPNET account has permission to create and write to files in the directory chosen for logging. If the user has write permissions and logging is still absent, you need to debug log4net; this luckily is alot easier than it sounds.

Firstly, we need to output all internal debugging messages; add to your web.config the following appSettings:

 <add key="log4net.Internal.Debug" value="true"/>

Internal debugging messages are written to the console and to the System.Diagnostics.Trace system. An ASP.Net application does not have a console, therefore messages will be will be lost. The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window). If the process does not have a debugger attached then the messages are sent to the system debugger. A utility like DebugView from may be used to capture these messages.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file (which is my preferred method). You can define a trace listener by adding the following to your application's web.config file (ensuring write permissions are enabled and dir exists):

<trace autoflush="true">
     initializeData="C:\tmp\log4net.txt" />

1 comment:

MAXIMUSPR007 said...

Thanks a lot! That did help me out!