Log4net实例

参考资料:http://sunxitao88.blog.163.com/blog/static/68314439200842882112105/

这里主要介绍web项目中的用法

1.修改AssemblyInfo.cs文件(Properties文件夹下面)

加入以下代码

[assembly: log4net.Config.DOMConfigurator(ConfigFile = "web.config", Watch = true)]

2.新建log.txt文件(放在根目录下面)

3.修改web.config文件,添加以下代码

 

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

 


  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="rollingFile" />
    </root>

 

    <appender  name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="false" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyy.MM.dd" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
        <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <logger name="Log4NetTest.LogTest">
      <level value="DEBUG" />
      <appender-ref  ref="rollingFile" />
      <appender-ref ref="coloredConsoleApp" />
      <appender-ref ref="SystemEvent" />
    </logger>
  </log4net>

 

4.运行代码

using System.IO;
using log4net;
using log4net.Config;

 ILog log = LogManager.GetLogger("LogTest");
 log.Info("This is a test!");

 

你可能感兴趣的:(log)