标签:style blog color io os ar 使用 for sp
log4net配置:
//Author:GaoBingBing [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
必备的组件:
1.log4net.dll
2.MySql.Data.dll
log4net.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- Author:GaoBingBing--> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <!--写入到数据库--> <appender name="ADONetAppender_DbServer" type="log4net.Appender.AdoNetAppender"> <!--错误队列数据达到5个才持久化到数据库--> <bufferSize value="1" /> <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/> <param name="ConnectionString" value="Server= 127.0.0.1;UID=root;PWD=root;DataBase=logs;Max Pool Size=1000"/> <commandText value="INSERT INTO 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>
辅助类:
using System; using System.Collections.Generic; using System.Linq; using System.Web; //Author:GaoBingBing public class Log { //获取实例 private static log4net.ILog myLogger = log4net.LogManager.GetLogger(Log4NetConfig.DBServer); //错误级别: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"); } }
//Author:GaoBingBing public class Log4NetConfig { public static string DBServer = "ADONetAppender_DbServer"; public static string File = "FileAppender"; }
就可以直接使用了!
标签:style blog color io os ar 使用 for sp
原文地址:http://www.cnblogs.com/gaobing/p/4031542.html