Log4Net section in Web.Config generates Error

后端 未结 2 1667
清歌不尽
清歌不尽 2020-12-19 02:25

So I am trying to set up Log4Net in my Web .NET 4.0 application. I have added the correct .dll to my project and have appended the following to my Web.Config file as starte

2条回答
  •  时光取名叫无心
    2020-12-19 03:15

    For developers who are not sure exactly how to get started following might be a help

    ConfigSections in app.config

    Remember to tell your application that a library is introducing a custom configuration section are you are intended to utilize, I am not perfectly sure if it is mandatory or not but I always use it as first section within root tag.

    
        

    log4net config in app.config

    There are quite a variety of different appenders available in log4net but I usually use RollingFileAppender so I am using the same one in this sample, you can find rest of those here.

    
        
        
          
          
          
          
          
          
          
            
          
        
        
          
          
        
      
    

    Update AssemblyInfo.cs file

    I always miss this step whenever I have to create a new project. So remember you have to tell your application to watch for XMLConfigurator to perform configuration of log4net, so following line goes at the end of AssemblyInfo.cs file:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    

    Get Started

    Remember to include the reference of log4net.dll then use following line of code to initialize logger within a class

    private static ILog log = LogManager.GetLogger(typeof(MyClass));
    

    And at the end lets use it like following

    log.Info("Hello log4net");
    

    Happy Logging :)

提交回复
热议问题