端游游戏开发

Log4Net记录到MySql

时间:2017-6-26 14:53:25  作者:06评比  来源:06评比  查看:9894  评论:0
内容摘要:1.新建控制台程序。2.添加Log4Net nuget 3.添加MySql 引用4.添加配置文件如下:<?xml version="1.0"?><configuration><configSections><...
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;

标签:Log4Net记录到MySql 
相关文章

    本站评比的所有娱乐站点,均为世界最权威专业的网站 

    06评比专为广大博友提供服务好、信誉高、种类多的娱乐网站 

    邮箱:1004003180@qq.com   合作广告请联系! 

    Copyright © 2006-2021  06评比,你身边的线上娱乐评比担保机构  All Rights Reserved