Log4Net records to MySql, Log4Net records MySql

Source: Internet
Author: User
Tags log4net

Log4Net records to MySql, Log4Net records MySql

1. Create a console program.

2. Add Log4Net nuget

3. Add MySql reference

4. Add the configuration file as follows:

<? Xml version = "1.0"?>

<Configuration>
<ConfigSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
</ConfigSections>

<Log4net>
<! -- Log displayed on the console -->
<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} [%-5 level] % m"/>
</Layout>

<Filter type = "log4net. Filter. LevelRangeFilter">
<Param name = "LevelMin" value = "Info"/>
<Param name = "LevelMax" value = "Fatal"/>
</Filter>
</Appender>
<! -- Database record log -->
<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>
<! -- (High) OFF> FATAL> ERROR> WARN> INFO> DEBUG> ALL (low) -->
<Level value = "ALL"/>
<Appender-ref = "ColoredConsoleAppender"/>
<Appender-ref = "ADONetAppender"/>
</Root>
</Log4net>

</Configuration>

 

5. Add Log4Net configuration in the console

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

6. Remember to copy log4netconfig. xml and the referenced MySql Library to the local machine.

7. The statement for creating a database table is as follows:

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;

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.