1,创建表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_general_ci NULL DEFAULT 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 utf8_general_ci NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=12
ROW_FORMAT=DYNAMIC
;
2,添加配置文件log4net.config
将下面内存复制到txt中,txt重命名为log4net.config,放在网站或程序根目录下。
<?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="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>
<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="ConversionPattern" 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,添加代码
新建一个类Log4net,代码如下:
public class Log4net
{
/// <summary>
/// 配置文件log4net.config是否加载
/// </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,添加动态库的引用。
(1)mysql.data.dll,用来连接mysql数据库的。
(2)log4net.dll,功能库。
可以从官网下载dll和源码。
http://logging.apache.org/log4net/download_log4net.cgi
5,使用
(1) Log4net.Error(string.Format("{0} 详细信息为:{1}", ex.Message, ex.ToString()));
(2)Log4net.Error(typeof(HomeController), string.Format("{0} 详细信息为:{1}", ex.Message, ex.ToString()));