微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

log4net用法

 

参考网上资料完成:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DAL
{
  public  class LogHelper
    {
        //Log4net支持多种级别的日志。优先级从高到低依次排列如下:FATAL > ERROR > WARN > INFO > DEBUG
      //日志发送到文件显示
         public static readonly log4net.ILog logInfo = log4net.LogManager.GetLogger("logerror");
      //日志发送到文件显示
      public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("loginfo");
      //日志发送到系统日志显示
      public static readonly log4net.ILog logevent = log4net.LogManager.GetLogger("logevent");

      //日志发送到sqlSERVER数据库
      public static readonly log4net.ILog logsqlServer = log4net.LogManager.GetLogger("logsqlServer");
     
      public static void SetConfig()
      {
         log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
      }
      public static void SetConfig(FileInfo configFile)
      {
          log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
      }


      public static void WritesqlServer(string info,Exception ex)
      {
          if(logsqlServer.IsInfoEnabled)
          {
              logsqlServer.Info(info,ex);
          }

          if (logsqlServer.IsErrorEnabled)
          {
              logsqlServer.Error(info,ex);
          }

          if(logsqlServer.IsFatalEnabled)
          {
              logsqlServer.Fatal(info,ex);
          }

          if(logsqlServer.IsWarnEnabled)
          {
              logsqlServer.Warn(info,ex);
          }

          if(logsqlServer.IsDebugEnabled)
          {
              logsqlServer.Debug(info,ex);
          }
      }

}

global.asax配置:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender,EventArgs e)
    {
        //在应用程序启动时运行的代码
        DAL.LogHelper.SetConfig();
    }
   
    void Application_End(object sender,EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }
       
    void Application_Error(object sender,EventArgs e)
    {
        //在出现未处理的错误时运行的代码
        Exception objExp = HttpContext.Current.Server.GetLastError();

    }

    void Session_Start(object sender,EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender,EventArgs e)
    {
        //在会话结束时运行的代码
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式
        //设置为 StateServer 或 sqlServer,则不会引发该事件。

    }
      
</script>

log4net.config信息

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

<configuration>

<configSections>
    <section name="log4net"  type="log4net.Config.Log4NetConfigurationSectionHandler" />
    </configSections>
 
  <log4net>
    <logger name="logerror">
      <level value="ERROR" />
      <appender-ref ref="ErrorAppender" />
    </logger>
    <logger name="loginfo">
      <level value="INFO" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <logger name="logevent">
      <level value="INFO" />
      <appender-ref ref="EventLogAppender" />
    </logger>
    <logger name="logsqlServer">
      <level name="INFO"></level>
      <appender-ref ref="adonetappender_sql"></appender-ref>
    </logger>
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="d:\\Error" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaxFileSize" value="10240" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p
   &lt;BR&gt;%n异 常 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" />
        <param name="Header" value="************BEGIN************" />
        <param name="Footer" value="************END************" />
      </layout>
    </appender>
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="d:\\Info" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p
   &lt;BR&gt;%n日 志 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" />
        <param name="Header" value="************BEGIN************" />
        <param name="Footer" value="************END************" />
      </layout>
    </appender>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="adonetappender_sql" type="log4net.Appender.adonetappender">
      <bufferSize value="10000" />
      <connectionType value="System.Data.sqlClient.sqlConnection,System.Data,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
      <connectionString value="DATABASE=MyDB;SERVER=.;UID=sa;PWD=1;Connect Timeout=15;" />
      <commandText value="INSERT INTO ErrorLog ([dtDate],[sThread],[sLevel],[sLogger],[sMessage],[sException]) VALUES (@log_date,@thread,@log_level,@logger,@message,@exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="100" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%t" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="200" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%p" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="500" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="3000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%m" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
  </log4net>  
</configuration>

DAL里的AssemblyInfo.cs里加一句

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐