Anubhav Goyal

Configuring Log4Net For Web Applications/Services

Advertisements

We all understand why we should use configuration files for log4net. The use of configuration files is specified @ http://logging.apache.org/log4net/release/manual/configuration.html. The config settings can either be added to the web.config file or we can create a seperate xml file say log4net.config.
I prefer to use a seperate config file. And the main reason is log4net framework will watch (using FileSystemWatcher) the external configuration file and will reload the config each time the file is modified. For some reason this powerful feature is not available if the settings are kept in web.config.
The other important class to understand is XmlConfigurator. This configurator must be set if a config file is used.
The default way to configure is log4net.Config.XmlConfigurator.Configure();
This makes the log4net look into the app.config or web.config file.
Configuration Attributes… read at the link specified
XmlConfiguratorAttribute The log4net.Config.XmlConfiguratorAttribute Allows the XmlConfigurator to be configured using the following properties:

If neither of the ConfigFile or ConfigFileExtension properties are specified, the web configuration file (web.config) will be used as the log4net configuration file. The System.Configuration API is only available if the configuration data is in the application’s config file; The file named MyApp.exe.config or Web.config.
Because the System.Configuration API does not support reloading of the config file the configuration settings cannot be watched using the log4net.Config.XmlConfigurator.ConfigureAndWatch methods. The main advantage of using the System.Configuration APIs to read the configuration data is that it requires less permissions than accessing the configuration file directly. The only way to configure an application using the System.Configuration APIs is to call the log4net.Config.XmlConfigurator.Configure() method or
the log4net.Config.XmlConfigurator.Configure(ILoggerRepository) method.

All that said let’s have a look how to put all this in place.

  1. Create a log4net.config file. This file will contain your logger settings. Please refer the official website for details how to create a valid config file. http://logging.apache.org/log4net
  2. Add the following attribute to your AssemblyInfo.cs file
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = “log4net.config”, Watch = true)]
    Alternately you can add the following line of code in the global.asax file in Application_Start Event.
    XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(HttpContext.Current.Request.PhysicalApplicationPath + “log4net.config”));
  3. Create the Logger
    private static readonly ILog log = LogManager.GetLogger(typeof(NameofYourClass));

    Its a good practice of create a seperate logger for each class. Then you can scope where the error occurred.
  4. Start logging.
    log.Error(exception.Message, exception.InnerException);
Advertisements

Advertisements