• Log4Net记录到MySql


    1.新建控制台程序。

    2.添加Log4Net nuget 

    3.添加MySql 引用

    4.添加配置文件如下:

    <?xml version="1.0"?>

    <configuration>
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

    <log4net>
    <!-- 控制台前台显示日志 -->
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
    <level value="ERROR" />
    <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
    <level value="Info" />
    <foreColor value="Green" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
    </layout>

    <filter type="log4net.Filter.LevelRangeFilter">
    <param name="LevelMin" value="Info" />
    <param name="LevelMax" value="Fatal" />
    </filter>
    </appender>
    <!--数据库记录日志-->
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="1" />
    <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
    <param name="ConnectionString" value="Server=localhost;port=3306;User ID=root;password=root;database=test;charset=utf8;Allow User Variables=True"/>
    <param name="CommandText" value="insert into log4net(log_datetime,log_thread,log_level,log_logger,log_message) values(@log_datetime, @log_thread , @log_level, @log_logger, @log_message)" />

    <param name="Parameter">
    <param name="ParameterName" value="@log_datetime" />
    <param name="DbType" value="DateTime" />
    <param name="Layout" type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    </param>
    </param>

    <param name="Parameter">
    <param name="ParameterName" value="@log_thread" />
    <param name="DbType" value="String" />
    <param name="Size" value="255" />
    <param name="Layout" type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%t" />
    </param>
    </param>
    <param name="Parameter">
    <param name="ParameterName" value="@log_level" />
    <param name="DbType" value="String" />
    <param name="Size" value="50" />
    <param name="Layout" type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%p" />
    </param>
    </param>
    <param name="Parameter">
    <param name="ParameterName" value="@log_logger" />
    <param name="DbType" value="String" />
    <param name="Size" value="255" />
    <param name="Layout" type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%c" />
    </param>
    </param>
    <param name="Parameter">
    <param name="ParameterName" value="@log_message" />
    <param name="DbType" value="String" />
    <param name="Size" value="10000" />
    <param name="Layout" type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%m" />
    </param>
    </param>
    </appender>
    <root>
    <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
    <level value="ALL" />
    <appender-ref ref="ColoredConsoleAppender"/>
    <appender-ref ref="ADONetAppender"/>
    </root>
    </log4net>

    </configuration>

    5.控制台程序添加Log4Net 配置

    log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4netconfig.xml"));

    6.切记 log4netconfig.xml 和 引用的MySql库要复制到本地。

    7.数据库建表语句如下:

    CREATE TABLE `log4net` (
    `log_datetime` datetime DEFAULT NULL,
    `log_thread` varchar(255) DEFAULT NULL,
    `log_level` varchar(255) DEFAULT NULL,
    `log_logger` varchar(255) DEFAULT NULL,
    `log_message` varchar(4000) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • 相关阅读:
    java file文件类操作使用方法大全
    java 中可以在方法中 新建 方法吗
    java InputStream读取数据问题
    file 创建方法
    java中File类的使用方法
    jquery怎么获取radio的值
    //初始化无限滚动分页组件
    表单提交 封装成json格式
    几个常用EL表达式的用法
    简单的顺序队列
  • 原文地址:https://www.cnblogs.com/GYY2046/p/7079424.html
Copyright © 2020-2023  润新知