Put your message here! Contact me for more information
 
 








 

Here is a quick guide to configure log4net for general asp.net 2.0 and asp.net 3.5 web projects. While you can find general guides for setting up log4net, more advanced configuration will require further digging through Log4Net documentations. My intention is to provide a quick, 5 minutes overview of configuring log4net for a web project with a more complicated, nested logging configuration.

My goal is to have 2 independent loggers working in parallel. One logs application errors (such as exceptions or serious errors that need to be reviewed later) to the Event log of Windows, and the other will log all general messages to a text file. The reason why 2 loggers are needed is because the Windows server is configured to send out email notifications on “Error” events to notify whoever in charge of maintaining the application. The general logging file containing all logging messages will provide more application-level debugging info.

Installing and Setting up Log4Net
Log4Net is a drop-in DLL file that you can download from the Apache project site. The binary is compatible with ASP.NET 3.5 and ASP.NET 2.0, as well as the Mono platform. After adding the reference to the DLL to your project, create the Global.asax file and add one line to the Application_Start method to initialize log4net at application start up.

void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
}

In the web.config file, add the log4net section handler to the configSections block



  
    

Basically we have 2 “appender” loggers and their above configurations are quite self-explanatory. The first appender, EventLogAppender, has a level range filter, which will filter out only FATAL or ERROR log entry. Then it has 2 mappings to map the different log levels to the correct Window’s Event message type. Finally, the layout section defines how the log text should look like.

For the 2nd logger, we use a RollingFile appender, which will automatically rotate the files based on the log file size. We define where to log the files to, what the maximum size is, and how many files we would like to keep. We don’t need to configure a filter for this logger since we are collecting all entries for debugging purposes, but you can add a range filter or a pattern filter without any problem.

Finally, we define the root logger. This root-level logger is the one logger receiving our log message and it will distribute these messages to all of the configured referenced appenders.

To use log4net in the program, all you need to do is add “using log4net;” to the top of the file, and initilize a static logger as follow:

public class PageBase : System.Web.UI.Page
{
  public static readonly ILog log = LogManager.GetLogger("Logger");
  public PageBase()
  {
     log.Debug("This is a debug message");
     log.Info("This is an info message");
     log.Error("This is an error message");
  }
}

Notes

  • Log4net is meant to be un-obtrusive and it won’t throw any error whatsoever, even in the case of invalid configuration. This means that you won’t know why it fails if log4net happens to fail. As the manual says, log4net is not meant to be a reliable logging mechanism. So watch out there
  • For the file appender, if you log into a sub-folder within the web project, you should take extra precaution steps to protect the one folder from public eyes. For example, denying all anonymous users from accessing the “log” folder by adding the following section into the web.config

  
    
      
        
      
    
  

References


Tags: , , ,

 

Leave a Reply