Asp.NET log4net 的用法

用户可以从http://logging.apache.org/log4net/下载log4net的源代码。解压软件包后,在解压的src目录下将log4net.sln载入Visual Studio .NET,编译后可以得到log4net.dll。用户要在自己的程序里加入日志功能,只需将log4net.dll引入工程即可.

 

web.config 中的配置:

 

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

加到 <configSections> 里面.

 

<log4net debug="true">
      <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
          <param name="File" value="c:/Applog.txt" />
          <param name="datePattern" value="yyyy-MM-dd HH:mm" />
          <param name="AppendToFile" value="true" />
          <layout type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
          </layout>
      </appender>
    <appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="C:/Log.txt" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="10" />
        <param name="MaximumFileSize" value="5MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="WARN" />
        <param name="LevelMax" value="WARN" />
        </filter>
    </appender>
     <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="RollingLogFileAppender" />
     </root>
  </log4net>

 

Global.asax  :

 

 void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码
        log4net.Config.DOMConfigurator.Configure(); //加上这个
    }

 

类中要用的话这样写:

public class Test

{

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

 

     public void Method1()

     {

          try

          {

          }

           catch(Exception ex){log.Error(ex.Message);}

     }

}

 

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭