Configuring Log4Net For Web Applications/Services

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:

  • ConfigFile If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain.CurrentDomain.BaseDirectory). This property cannot be used in conjunction with the ConfigFileExtension property.
  • ConfigFileExtension If specified, this is the extension for the configuration file. The assembly file name is used as the base name with the this extension appended. For example if the assembly is loaded from the a file TestApp.exe and the ConfigFileExtension property is set to log4net then the configuration file name is TestApp.exe.log4net. This is equivalent to setting the ConfigFile property to TestApp.exe.log4net. The path to the configuration file is build by using the application base directory (AppDomain.CurrentDomain.BaseDirectory), the assembly file name and the configuration file extension.  This property cannot be used in conjunction with the ConfigFile property.
  • Watch If this flag is specified and set to true then the framework will watch the configuration file and will reload the config each time the file is modified.

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