label:
1. Create the table log CREATE TABLE `log` (` id` int (11) NOT NULL AUTO_INCREMENT, `log_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,` log_thread` varchar (255) CHARACTER SET utf8 COLLATE utf8_geneUL_NULL NULL `log_level` varchar (255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,` log_logger` varchar (255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `log_message` text CHARACTER SET utf8 COLLATE UTf8generate ) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci AUTO_INCREMENT = 12 ROW_FORMAT = DYNAMIC;
2. Add the configuration file log4net.config. Copy the following memory into txt. Rename txt to log4net.config and place it in the website or program root directory.
<? xml version = "1.0"?> <configuration> <configSections> <section name = "log4net" type = "log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </ configSections> <log4net> <appender name = "stdout" type = "log4net.Appender.ConsoleAppender"> <layout type = "log4net.Layout.PatternLayout"> <param name = "ConversionPattern" value = "[% d% l]% m% n" /> </ layout> < / appender> <appender name = "ADONetAppender" type = "log4net.Appender.ADONetAppender"> <param name = "ConnectionType" value = "MySql.Data.MySqlClient.MySqlConnection, MySql.Data" /> <param name = "ConnectionString "value =" Server = 192.168.16.247; port = 3306; User ID = root; password = Juanpi123; database = spider; charset = utf8; Allow User Variables = True "/> <param name =" CommandText "value =" insert into log (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 =" Paramet erName "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"> <par am 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> <appender name = "file" type = "log4net.Appender.RollingFileAppender"> <param name = "File "value =" log \\ log "/> <param name =" AppendToFile "value =" true "/> <param name =" RollingStyle "value =" Date "/> <param name =" DatePattern "value =" yyyy -MM-dd "/> <layout type =" log4net.Layout.PatternLayout "> <param name =" ConversionPatt ern "value =" [% d% l]% m% n "</ layout> </ appender> <root> <level value =" ALL "/> <appender-ref ref =" ADONetAppender "/> < / root> <logger name = "*"> <level value = "ALL" /> <appender-ref ref = "ADONetAppender" </ logger> </ log4net> </ configuration>
3. Add code Create a new class Log4net, the code is as follows:
public class Log4net {/// <summary> /// Whether the configuration file log4net.config is loaded // // </ summary> public static readonly bool IsConfigLoaded = false; public static string ConfigFileName = "log4net.config"; private static log4net. ILog logger = LogManager.GetLogger (typeof (Log4net)); static Log4net () {string path = System.Web.HttpContext.Current.Server.MapPath (ConfigFileName); if (File.Exists (path)) {log4net.Config. XmlConfigurator.ConfigureAndWatch (new FileInfo (path)); IsConfigLoaded = true;}} public static void Error (string message) {if (IsConfigLoaded) {logger.ErrorFormat (message);}} public static void Error (Type type, string message ) {if (IsConfigLoaded) {log4net.
ILog logger = LogManager.GetLogger (type); logger.ErrorFormat (message);}}}
4. Add a reference to the dynamic library.
(1) mysql.data.dll, used to connect to the mysql database.
(2) log4net.dll, function library. You can download the dll and source code from the official website. http://logging.apache.org/log4net/download_log4net.cgi
5. Use
(1) Log4net.Error (string.Format ("{0} details are: {1}", ex.Message, ex.ToString ()));
(2) Log4net.Error (typeof (HomeController) , string.Format ("{0} details are: {1}", ex.Message, ex.ToString ()));
log4net write mysql complete example