Thursday, December 19, 2013

log4net configuration - one file per month



You want to have an external log4net.config file and also need one log file per month.

using log4net;
public class MyClass
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(MyClass));

    public Test()
    {
        //For debugging, To check if log4net configured correctly
        bool log4netIsConfigured = log4net.LogManager.GetRepository().Configured;

        Logger.Info("This is my log");
    }
}

You need to add an appSettings key called "log4net.Config" and then you can put an app-relative path to an external log4net.config file in there and everything will automatically works.
That file path is relative to the application base directory (AppDomain.CurrentDomain.BaseDirectory), so if you put the external log4net file in the root of the application, for console applications, file path would be "../../log4net.config" and for web applications, It would be "log4net.config".

web.config

  
    
  


log4net.config

    
      
       
      
            
       
      
       
      
      
        
      
    
    
      
      
    
  

0 comments:

Post a Comment