Log4net配置
一 AssemblyInfo.cs文件 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
二 创建log4net.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--写入到数据库-->
<appender name="ADONetAppender_DbServer" type="log4net.Appender.AdoNetAppender">
<!--错误队列数据达到5个才持久化到数据库-->
<bufferSize value="1" />
//mysql使用
<param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
//sqlserver使用
<param name="ConnectionType" value="System.Data.SqlClient.SqlConnection, System.Data,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<param name="ConnectionString" value="Data Source=559a75624d857.sh.cdb.myqcloud.com;port=6850;initial catalog=xiaoyao_blogs;user id=cdb_outerroot;password=7391428wang;pooling=true;max pool size=100;Charset=utf8"/>
<commandText value="INSERT INTO xiaoyao_blogs_log(Date,Thread,Level,Logger,Message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
<parameter>
<parameterName value="@log_date"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</layout>
<!-- <layout type="log4net.Layout.RawTimeStampLayout"/>-->
</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>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="ADONetAppender_DbServer"/>
</root>
<!-- Specify the level for some specific categories -->
<logger name="iNotes">
<!-- <appender-ref ref="B" /> -->
<level value="ALL"/>
<appender-ref ref="ADONetAppender_DbServer"/>
</logger>
</log4net>
</configuration>
三 创建Log.cs 文件 public class Log { //获取实例 private static log4net.ILog myLogger = log4net.LogManager.GetLogger(Log4NetConfig.DBServer); static Log() { } //错误级别:Info public static void Info(Exception ex, string message) { myLogger.Info(AppendMessage(ex, message)); } //错误级别:Debug public static void Debug(Exception ex, string message) { myLogger.Debug(AppendMessage(ex, message)); } //错误级别:Warn public static void Warn(Exception ex, string message) { myLogger.Warn(AppendMessage(ex, message)); } //错误级别:Fatal public static void Fatal(Exception ex, string message) { myLogger.Fatal(AppendMessage(ex, message)); } //错误级别:Error public static void Error(Exception ex, string message) { myLogger.Error(AppendMessage(ex, message)); } //拼接错误信息 private static string AppendMessage(Exception ex, string message) { return "自定义错误信息:" + message + "。系统错误信息:" + ex.Message + ",发生时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } }
四 创建Log4NetConfig.cs文件 public class Log4NetConfig { public static string DBServer = "ADONetAppender_DbServer"; public static string File = "FileAppender"; }
五,修改Global.asax文件
添加 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
六,根据log4net.config里的数据来创建数据库
七,必须添加的类库 log4net.dll,MySql.Data.dll,并且引用
八,必须把 log4net.dll,MySql.Data.dll复制一份放到bin文件夹下,否则不会写日志
九 ,本文来源于:http://www.cnblogs.com/gaobing/p/3809526.html