log4net with ASP.NET MVC: nothing happens

后端 未结 4 1808
挽巷
挽巷 2020-12-24 07:11

I am trying to use log4net with ASP.NET MVC and I cannot get anything to happen with it. I\'ve created a config that is in my web project root:



        
相关标签:
4条回答
  • 2020-12-24 07:21

    Confuguring Log4Net in ASP.NET MVC 4 Web Application

    1. Download Log4Net:

      • http://logging.apache.org/log4net/download.html
    2. Add reference in ASP.Net MVC 4 application:

      1. Solution Explorer
      2. Right click on project-name
      3. Add references
      4. Browse Log4Net.dll
    3. Modify Web.config file (source):

    <configuration> 
        <configSections>
            <section
                name="log4net"
                type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
        </configSections>
        <log4net>
            <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
                <param name="File" value="LOGS\log.txt"/>
                <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
                <appendToFile value="true" />
                <rollingStyle value="Size" />
                <maxSizeRollBackups value="2" />
                <maximumFileSize value="10MB" />
                <staticLogFileName value="true" />
                <layout type="log4net.Layout.PatternLayout">
                    <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
                </layout>
            </appender>
            <root>
                <level value="ALL" />
                <appender-ref ref="LogFileAppender" />
            </root> 
            <logger name="test">
                <level value="DEBUG"></level>
            </logger>
        </log4net>
    </configuration>
     
    
    1. Modify AssemblyInfo.cs:
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
    
    1. Modify global.asax.cs:
    protected void Application_Start()
    {
        log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
    }
    
    1. Add logger declaration in class for which you want to make logs:
    readonly log4net.ILog log =
        log4net.LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
        );
    
    1. Use log functions:
    log.Error("make mistakes");
    

    Further reading: http://anandsonake.blogspot.in/2013/08/log4net-in-aspnet-mvc.html

    0 讨论(0)
  • 2020-12-24 07:25

    Ensure that you call

    log4net.Config.XmlConfigurator.Configure();
    

    At the startup of your application.

    0 讨论(0)
  • 2020-12-24 07:25

    Add below line in AssemblyInfo.cs :

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

    0 讨论(0)
  • 2020-12-24 07:31

    Do you have something like this in global.asax?

    void Application_Start(object sender, EventArgs e)
    {
        string l4net = Server.MapPath("~/log4net.config");
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
    
        ...
    
    0 讨论(0)
提交回复
热议问题