直接上代碼示例:https://share.weiyun.com/aff36f2547514cfefe129ebb8ccb28efhtml
首先添加加log4net的dll,推薦用nuget....mysql
貼上配置文件sql
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4Net" /> </configSections> <!--日誌的配置開始--> <log4net debug="true"> <root> <level value="ALL" /> <appender-ref ref="SysAppender" /> <appender-ref ref="AdoNetAppender" /> </root> <logger name="WebLogger"> <!--配置日誌的級別,低於此級別的就不寫到日誌裏面去--> <level value="DEBUG" /> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="App_Data/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".htm"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="<HR COLOR=red>%n異常時間:%d [%t] <BR>%n異常級別:%-5p <BR>%n異 常 類:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> <!--<conversionPattern value="%newline %n記錄時間:%date %newline %n 線程ID:[%thread] %n日誌級別: %-5level %n跟蹤描述:%message%newline %n"/>--> </layout> </appender> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> <connectionString value="server=localhost;port=3306;database=jianzheng;uid=root;pwd=svse;" /> <commandText value="INSERT INTO `TestSysLog` (`Date`, `Thread`, `Level`, `Logger`, `Method`, `Location`,`Message`, `Exception`) VALUES (@Date, @Thread, @Level, @Logger, @Method,@Location,@Message, @Exception)" /> <parameter> <parameterName value="@Date" /> <dbType value="DateTime" /> <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="@Level" /> <dbType value="String" /> <size value="255" /> <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="@Method" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%method" /> </layout> </parameter> <parameter> <parameterName value="@Location" /> <dbType value="String" /> <size value="1023" /> <layout type="log4net.Layout.PatternLayout" > <conversionPattern value="%location" /> </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="8000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> </log4net> <!--日誌的配置結束--> </configuration>
本demo裏是新建的一個xml文件。
在網站根目錄新建如圖:
把xml配置內容copy進去,
在Global.asax的Application_Start()中加入下面的一句
//讀取日誌 若是使用log4net,應用程序一開始的時候,都要進行初始化配置
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("config\\log4net.config"));
在AssemblyInfo.cs里加入
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config\\log4net.config", Watch = false)]
AssemblyInfo.cs的位置在如圖:
而後添加一個過濾器
public class MyExceptionFileAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { base.OnException(filterContext); //處理錯誤消息,將其跳轉到一個頁面 LogHelper.WriteLog(filterContext.Exception.ToString()); //頁面跳轉到錯誤頁面 filterContext.HttpContext.Response.Redirect("/Error.html"); } }
還要建一個工具類,用來寫日誌。數據庫
public class LogHelper
{
public static void WriteLog(string txt)
{
ILog log = LogManager.GetLogger("log4netlogger");
log.Error(txt);
}
}
最後還要在app
FilterConfig.cs中加入全局過濾器
//註冊錯誤過濾器
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
//註冊錯誤過濾器
filters.Add(new MyExceptionFileAttribute());
}
默認把錯誤日誌存在app_data中,爲htm文件。
存到mysql數據庫中了
數據庫結構如圖:
注意:Mysql.Data與Connector版本都要是6.8.3才能成功。其餘版本須要自行測試了ide
connectionType:見:http://www.cnblogs.com/gaocong/p/4699103.html工具