Tuesday, March 25, 2014

How to add log4net into ASP.NET project?

1. Download dll file from
http://logging.apache.org/log4net/download_log4net.cgi
2.Add following log4net.config file into project


  
    
      
      
      
      
      
      
      
        
      
    
    
      
        
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
      
    
    
      
      
      
    
  

3. In Global.asax
protected void Application_Start()
        {
            string l4net = Server.MapPath("~/log4net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
        }


4. Add write permission setting for log file folder
IIS manager -> Application pool -> Get name of application pool
File explorer -> folder property -> Security -> Add IIS AppPool\\app pool name into log file folder write/read permission.

http://blogs.iis.net/webdevelopertips/archive/2009/10/02/tip-98-did-you-know-the-default-application-pool-identity-in-iis-7
-5-windows-7-changed-from-networkservice-to-apppoolidentity.aspx

5. Usage:
ILog log = LogManager.GetLogger(typeof(this));
log.Info("message");

References:
Apache log4net™ Manual - Configuration
http://logging.apache.org/log4net/release/manual/configuration.html
http://ruchirac.blogspot.ca/2012/10/configure-log4net-with-aspnet-logging.html