Configure Log4Net for ASP.NET 2.0 Web Application...

Just encountered this problem right now and quickly write the solution here. Should be an old info since ASP.NET 2.0 had been out for a while.

The problem is that I want to use Log4Net on a testing web application to get some log data for analysis. I just downloaded the latest version of Log4Net and followed the way I used in previous projects which was an ASP.NET 1.1 web application,  trying to get Log4Net to run in an ASP.Net 2.0 web app. And it's not running as expected.

the steps to let Log4Net run in ASP.Net 1.1 are:

  1. writing config section for Log4Net in web.config file
  2. insert logging code in conponents and web page code-behind
  3. At website's AssemblyInfo.cs or .vb file, add "<Assembly: log4net.Config.DOMConfigurator(Watch:=True)>" for VB.net or "[assembly: log4net.Config.XmlConfigurator(Watch=true)]" for C# to enable Log4Net to Log stuffs.
  4. compile the web application and run.

By searching and refering the posts here and here and here, comes out that the configuration way in ASP.NET 2.0 is little bit different, as follows:

  1. writing config section for Log4Net in web.config file
  2. insert logging code in conponents and web page code-behind
  3. At Global.asax file, under Application_Start event, add "log4net.Config.XmlConfigurator.Configure()" to trigger Log4Net to log stuffs.
  4. compile the web application and run.

By doing the settings above, Log4Net started to output logs in my ASP.Net 2.0 application.

just one more thing to notice, remember to enable the write permission to "ASPNET" and/or "NETWORK SERVICE" accounts if you still can not get the log file by doing above settings. it may be the file writing permission problems.

FYI.

Technorati tags: microsoft, asp.net, programming, log4net, .net