码迷,mamicode.com
首页 > Web开发 > 详细

Log4Net基础配置

时间:2018-08-14 12:12:01      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:测试   不同   接口   command   ken   end   app   open   content   

一.组件引用

 1.首先nuget中引入log4net

 2.项目AssemblyInfo.cs引入log4net,  文件路径:项目/Properties/AssemblyInfo.cs

   注:如果不在此引入直接在global中引入,在不部署到IIS中时本地测试是可以用的,如果要部署到IIS中则需要添加,请注意。

技术分享图片

 

3.global中引入 

   

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));

 

二.log4net.config配置

1.写入文件

  注意:如果是有多个appender写文件配置,如果记录则会同时写入多个appender指定的文件中,如需解决此问题,需要看底层代码重写,目前暂时用不到所以没去处理。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <log4net>
    <!--定义输出到文件中-->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--输出文件路径-->
      <file type="log4net.Util.PatternString"  value="Log/%date{yyyy}/%date{yyyyMM}/"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd‘.txt‘"/>
      <staticLogFileName value="false"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--每条日志末尾的文字说明-->
        <!--输出格式-->
        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n类:%logger property: [%property{NDC}] - %n描述:%message%newline %n"/>
      </layout>
    </appender>
    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="ALL"/>
      <!--文件形式记录日志-->
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
</configuration>

2.写入数据库

  注意点:

  1. 写入数据库appender type类型为:log4net.Appender.AdoNetAppender
  2. 可以为多种级别信息创建不同的appender,以name区分
  3.  请注意每添加一个appender,需要增加过滤器用于识别logger,同时需要增加DenyAllFilter,防止其他appender也插入日志,loggerToMatch中value对应代码中 logger别名。
 <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="gops" />
  </filter>
 <filter type="log4net.Filter.DenyAllFilter" />

LogHelper类:

 /// <summary>
    /// 日志辅助类
    /// </summary>
   public class LogHelper
    {
       private static log4net.ILog log = null;
       static LogHelper()
       {
           log = log4net.LogManager.GetLogger("gops");
       }

       /// <summary>
       /// 普通日志
       /// </summary>
       /// <param name="content"></param>
       public static void Info(string content)
       {
           try
           {
               log.Info(content);
           }
           catch { }
       }

       /// <summary>
       /// 调试日志
       /// </summary>
       /// <param name="content"></param>
       public static void Debug(string content, Exception ex = null)
       {
           try
           {
               log.Debug(content,ex);
           }
           catch { }
       }

       /// <summary>
       /// 错误日志
       /// </summary>
       /// <param name="content"></param>
       public static void Error(string content,Exception ex=null)
       {
           try
           {
               log.Error(content,ex);
           }
           catch { }
       }

       /// <summary>
       /// 严重错误日志
       /// </summary>
       /// <param name="content"></param>
       public static void Fatal(string content, Exception ex = null)
       {
           try
           {
               log.Fatal(content,ex);
           }
           catch { }
       }

       /// <summary>
       /// 警告日志
       /// </summary>
       /// <param name="content"></param>
       public static void Warn(string content, Exception ex = null)
       {
           try
           {
               log.Warn(content,ex);
           }
           catch { }
       }
    }

 

完整代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <log4net>
    <!--<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection,System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" />
      <connectionString value="数据库连接" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 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="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="gops1" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>-->

    <!--接口日志-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection,System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" />
      <connectionString value="数据库连接" />
      <commandText value="execute LogInsert @tablename,@thread,@log_level,@logger,@message,@exception" />
      <parameter>
        <parameterName value="@tablename" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date{OpenApiyyyyMM}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
      <!--请注意每添加一个appender,需要增加过滤器用于识别logger,同时需要增加DenyAllFilter,防止其他appender也插入日志-->
      <!--<filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="OpenApiLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />-->
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="gops" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="all" />
      <appender-ref ref="AdoNetAppender"/>
      <!--<appender-ref ref="OpenApiAppender"/>-->
    </root>
  </log4net>
</configuration>

 

Log4Net基础配置

标签:测试   不同   接口   command   ken   end   app   open   content   

原文地址:https://www.cnblogs.com/Luis-Gong/p/9473686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!