首页 > 代码库 > web端log4net输出错误日志到mysql

web端log4net输出错误日志到mysql

1.配置log4net.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Author:GaoBingBing-->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <!--写入到数据库-->
    <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender">
      <!--错误队列数据达到5个才持久化到数据库-->
      <bufferSize value="http://www.mamicode.com/5" />
      <param name="ConnectionType" value="http://www.mamicode.com/MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
      <param name="ConnectionString" value="http://www.mamicode.com/server=localhost;database=test;Uid=sa;Pwd=sa;old syntax=yes"/>
      <commandText value="http://www.mamicode.com/INSERT INTO log(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_date"/>
        <dbType value="http://www.mamicode.com/DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
        <!--<layout type="log4net.Layout.PatternLayout" value="http://www.mamicode.com/%date{yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss}" />-->
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@thread"/>
        <dbType value="http://www.mamicode.com/String"/>
        <size value="http://www.mamicode.com/255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_level"/>
        <dbType value="http://www.mamicode.com/String"/>
        <size value="http://www.mamicode.com/50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@logger"/>
        <dbType value="http://www.mamicode.com/String"/>
        <size value="http://www.mamicode.com/255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@message"/>
        <dbType value="http://www.mamicode.com/String"/>
        <size value="http://www.mamicode.com/4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message"/>
        </layout>
      </parameter>
      <!--<parameter>
        <parameterName value="http://www.mamicode.com/@exception"/>
        <dbType value="http://www.mamicode.com/String"/>
        <size value="http://www.mamicode.com/2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>-->
    </appender>
    <root>
      <level value="http://www.mamicode.com/ALL"/>
      <appender-ref ref="AdoNetAppender_MySql"/>
    </root>
    <logger name="*">
      <level value="http://www.mamicode.com/ALL" />
      <appender-ref ref="AdoNetAppender_MySql" />
    </logger>
  </log4net>
</configuration>

2.项目启动处

log4net.Config.XmlConfigurator.Configure();

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

4.调用log4net

log4net.ILog myLogger = log4net.LogManager.GetLogger("message");

myLogger.Info("hello!");

简单四步配置成功第一次写博客园,还没有掌握好看的布局,再接再厉!嘻嘻

 

web端log4net输出错误日志到mysql