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="5" />
      <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
      <param name="ConnectionString" value="server=localhost;database=test;Uid=sa;Pwd=sa;old syntax=yes"/>
      <commandText value="INSERT INTO log(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
        <!--<layout type="log4net.Layout.PatternLayout" value="%date{yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss}" />-->
      </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>
    <root>
      <level value="ALL"/>
      <appender-ref ref="AdoNetAppender_MySql"/>
    </root>
    <logger name="*">
      <level value="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

上一篇:mongodb设置访问权限


下一篇:应用索引技术优化SQL 语句三