zoukankan      html  css  js  c++  java
  • Log4net从下载到使用例子

    一、首先下载log4net.dll   http://pan.baidu.com/s/1gdigrwJ

    二、添加log4net引用

    三、代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using log4net;
    using System.Reflection;
    
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                //记录错误日志
                log.Error("error", new Exception("发生了一个异常"));
                //记录严重错误
                log.Fatal("fatal", new Exception("发生了一个致命错误"));
                //记录一般信息
                log.Info("info");
                //记录调试信息
                log.Debug("debug");
                //记录警告信息
                log.Warn("warn");
            }
        }
    }

    四、config匹配

    <log4net>
      <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
      <!-- Set root logger level to ERROR and its appenders -->
      <root>
        <level value="ALL"/>
        <appender-ref ref="SysAppender"/>
      </root>
    
      <!-- Print only messages of level DEBUG or above in the packages -->
      <logger name="WebLogger">
        <level value="DEBUG"/>
      </logger>
    
      <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
        <param name="File" value="App_Data/" />
        <param name="AppendToFile" value="true" />
        <param name="RollingStyle" value="Date" />
        <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
        <param name="StaticLogFileName" value="false" />
        <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>
    </log4net>

     五、如果想让log4net的config单独成log4net.config可以在程序集信息配置

    代码:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "xml", Watch = true)]

  • 相关阅读:
    Flask 入门(五)
    Mac 系统root
    Flask 入门(四)
    Flask 入门(三)
    Flask 入门(二)
    Flask 入门(一)(Mac 系统)
    linux升级python2.7到3.7.0
    [20170825]11G备库启用DRCP连接3.txt
    [20170824]11G备库启用DRCP连接.txt
    [20170728]oracle保留字.txt
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3488666.html
Copyright © 2011-2022 走看看