log4net项目中如何配置,以下详解

log4net.config配置文件

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="false">

  <appender name="RootAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <param name="Encoding" value="utf-8" />
    <staticLogFileName value="false" />
    <file value="E:\App\log\" />
    <datePattern value="LOG yyyyMMdd.LOG" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="记录时间:%date%n线程ID:[%thread]%n日志级别:%-5level%n记录类:%logger%n操作者ID:%property{Operator} 操作类型:%property{Action}%n当前机器名:%property%n当前机器名及登录用户:%username %n记录位置:%location%n消息描述:%property{Message}%n异常:%exception%n消息:%message%newline%n%n" />

    </layout>
  </appender>

  <root>
    <appender-ref ref="RootAppender" />
  </root>

</log4net>

Global.asax中Application_Start方法加载log4net.config配置文件

    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("log4net.config")));

    }

实例化log

ILog log = LogManager.GetLogger("DataService.aspx");

catch中调用log写入

log.Error("错误信息:" + ex.Message, ex);

 

posted @ 2015-10-27 15:58  bkyshj  阅读(304)  评论(0编辑  收藏  举报