log4net Configuration for Multiple Files

Kailash Chandra Behera | Wednesday, July 29, 2020


If you are a dot net developer, you must have worked in the log4net config to write your logging messages into the file. Here in this blog, we will see how to do log4net configuration to write logs in multiple files with log4net c# example.

Getting Started

Recently I was working on a project where I had to write the error logs and warnings in two different text files. I have the only idea how to use log4net config to write logs in a file but I don't have an idea how to do log4net configuration for multiple files, there are very few articles available on the internet for this.

After lots of browsing, I got the idea to write logs in multiple files. It's very easy, just two more steps required for that. here in the below log4net c# and log4net configuration file code snippet states how to log.

Before continuing this, you must have ideas on how to use log4net. if you are totally new then click on the below link to know how to use log4net.

Related Articles

  1. Log4net with MVC
  2. Log4net with WPF
  3. Log4net with Console
  4. Log4net in C#

Go to your application where you want to implement the multiple file logger, add one more appender(here I have taken the example of rollingfileappnder) and a logger.your log4net configuration should look like the below example.

 <?xml version="1.0" encoding="utf-8" ?>  
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />  
   <appender name="DefaultAppender" type="log4net.Appender.RollingFileAppender">  
    <file value="Logs\DefaultAppender.log" />  
    <encoding value="utf-8" />  
    <appendToFile value="true" />  
    <rollingStyle value="Date" />  
    <layout type="log4net.Layout.PatternLayout">  
     <conversionPattern value="%date- %message%n" />  
   <appender name="AlternetAppender" type="log4net.Appender.RollingFileAppender">  
    <file value="Logs\AlternetAppender.data" />  
    <encoding value="utf-8" />  
    <appendToFile value="true" />  
    <rollingStyle value="Date" />  
    <layout type="log4net.Layout.PatternLayout">  
     <conversionPattern value="%date- %message%n" />  
    <level value="All" />  
    <!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->  
    <appender-ref ref="DefaultAppender" />  
   <logger additivity="false" name="warning">  
    <level value="All" />  
    <appender-ref ref="AlternetAppender" />  

The above code example in the config file declares two log4net appenders for two different files (see the file path of both appenders), the first appender is default appender and the second appender is alternate appender is for writing warnings only.

The additivity in logger provides the facility to select the appender manually at run time to write log4net logs in a different file (not default one). Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used. However, the children of this logger will inherit its appenders, unless the children have their additivity flag set to false too. See the user manual for more details.

Then declare two different objects of the ILog in your code like in the Main method of below code. Here these are the only examples to show you how to use two different appenders to write logs in two different files. You can write according to your requirement.

 static class Program  
     /// <summary>  
     /// The main entry point for the application.  
     /// </summary>  
     static void Main()  
                log4net.ILog defaultLogger=log4net.LogManager.GetLogger(typeof(Logger));  
                //Will write logs in the file whose path is provide in the default appender   
                defaultLogger.Error("Error Log");  
                log4net.ILog altLogger= log4net.LogManager.GetLogger("AlternetAppender");  
                //Will write log in file whose path is provided in alternet appender  
                altLogger.Warn("Warning Log");  

The first object is the default object and created by providing the class name as a parameter what we generally doing while working with log4net and by default, it uses the first appender to write log. The second logger is created by providing the appender name, here we forcefully tell the log4net to use alternate appender to write log.


I hope you have got idea how to do log4net Configuration for multiple files appender and enjoyed it a lot.