zoukankan      html  css  js  c++  java
  • web端log4net输出错误日志到mysql

    1.引用log4net

    2.配置log4net.config文件

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3   <!-- Author:GaoBingBing-->
     4   <configSections>
     5     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
     6   </configSections>
     7   <log4net>
     8     <!--写入到数据库-->
     9     <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender">
    10       <!--错误队列数据达到5个才持久化到数据库-->
    11       <bufferSize value="5" />
    12       <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
    13       <param name="ConnectionString" value="server=localhost;database=test;Uid=sa;Pwd=sa;old syntax=yes"/>
    14       <commandText value="INSERT INTO log(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
    15       <parameter>
    16         <parameterName value="@log_date"/>
    17         <dbType value="DateTime"/>
    18         <layout type="log4net.Layout.RawTimeStampLayout"/>
    19         <!--<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss}" />-->
    20       </parameter>
    21       <parameter>
    22         <parameterName value="@thread"/>
    23         <dbType value="String"/>
    24         <size value="255"/>
    25         <layout type="log4net.Layout.PatternLayout">
    26           <conversionPattern value="%thread"/>
    27         </layout>
    28       </parameter>
    29       <parameter>
    30         <parameterName value="@log_level"/>
    31         <dbType value="String"/>
    32         <size value="50"/>
    33         <layout type="log4net.Layout.PatternLayout">
    34           <conversionPattern value="%level"/>
    35         </layout>
    36       </parameter>
    37       <parameter>
    38         <parameterName value="@logger"/>
    39         <dbType value="String"/>
    40         <size value="255"/>
    41         <layout type="log4net.Layout.PatternLayout">
    42           <conversionPattern value="%logger"/>
    43         </layout>
    44       </parameter>
    45       <parameter>
    46         <parameterName value="@message"/>
    47         <dbType value="String"/>
    48         <size value="4000"/>
    49         <layout type="log4net.Layout.PatternLayout">
    50           <conversionPattern value="%message"/>
    51         </layout>
    52       </parameter>
    53       <!--<parameter>
    54         <parameterName value="@exception"/>
    55         <dbType value="String"/>
    56         <size value="2000"/>
    57         <layout type="log4net.Layout.ExceptionLayout"/>
    58       </parameter>-->
    59     </appender>
    60     <root>
    61       <level value="ALL"/>
    62       <appender-ref ref="AdoNetAppender_MySql"/>
    63     </root>
    64     <logger name="*">
    65       <level value="ALL" />
    66       <appender-ref ref="AdoNetAppender_MySql" />
    67     </logger>
    68   </log4net>
    69 </configuration>

    2.项目启动处 Global.asax

    log4net.Config.XmlConfigurator.Configure();

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

    4.创建log.cs

     1  //获取实例
     2     private static log4net.ILog myLogger = log4net.LogManager.GetLogger(Log4NetConfig.MySql);
     3 
     4     //错误级别:Info
     5     public static void Info(string message)
     6     {
     7         myLogger.Info(message);
     8     }
     9     //错误级别:Debug
    10     public static void Debug(string message)
    11     {
    12 
    13         myLogger.Debug(message);
    14     }
    15     //错误级别:Warn
    16     public static void Warn(string message)
    17     {
    18 
    19         myLogger.Warn(message);
    20     }
    21     //错误级别:Fatal
    22     public static void Fatal(string message)
    23     {
    24 
    25         myLogger.Fatal(message);
    26     }
    27     //错误级别:Error
    28     public static void Error(string message)
    29     {
    30 
    31         myLogger.Error(message);
    32     }

    5.调用log4net

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

    myLogger.Info("hello!");

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

  • 相关阅读:
    ORACLE复制数据库【weber出品】
    AJAX和jquery简单试用
    git 基本命令大全
    git使用技巧
    listagg( ) within group ( order by ) 与 wm_concat
    oracle 数据库查询多条数据的一列值
    Fstdfs +nginx 安装详细步骤
    解决Oracle用户被锁定的方法
    解决tomcat内存溢出
    PowerDesigner将PDM导出生成WORD文档
  • 原文地址:https://www.cnblogs.com/zhhying/p/3990866.html
Copyright © 2011-2022 走看看