Using Log4Net's output log to MySQL
The operation steps are as follows:
1. Add reference Log4Net.dll, mysql.data.dll to the project
note: the version of mysql.data.dll should be higher. It is better to download the latest version (6.2.7) from MySQL official website. My lesson: I used the lower version of mysql.data.dll, so I couldn't insert the data into the MySQL database, and the content of the insert table was NULL, indicating that the System.data.dll was loaded incorrectly.
2. Add app.config to the project
app.config is configured as follows:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1"/> <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/> <param name="ConnectionString" value="server=localhost;database=test;Uid=root;Pwd=root;"/> <commandText value="INSERT INTO log4net(log_datetime,log_thread,log_level,log_logger,log_message,Exception) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"/> <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>
3. Add the following statement to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]
4. call in program
log4net.ILog log = log4net.LogManager.GetLogger("MyLogger"); log.Info("hello world!");
5. You can check whether a Message: hello world has been successfully inserted into mysql database.
Re: the above is a personal summary for reference.
Reprinted at: https://www.cnblogs.com/sql4me/p/3254134.html