之前在討論區有看到這樣的問題..去找了一下資料..教大家如何記錄Exception訊息
記錄網頁Exception訊息的方法有很多種..在此介紹三種儲存方式..
1.記錄在事件檢視器裡
2.記錄在文字檔裡
3.用Email寄出訊息
asp.net(c#)
Global.asax
<%@ Application Language="C#" %> <script RunAt="server"> void Application_Start(object sender, EventArgs e) { // 應用程式啟動時執行的程式碼 } void Application_End(object sender, EventArgs e) { // 應用程式關閉時執行的程式碼 } void Application_Error(object sender, EventArgs e) { string Message = ""; Exception ex = Server.GetLastError(); Message = "發生錯誤的網頁:{0}錯誤訊息:{1}堆疊內容:{2}"; Message = String.Format(Message, Request.Path + Environment.NewLine, ex.GetBaseException().Message + Environment.NewLine, Environment.NewLine + ex.StackTrace); //寫入事件撿視器,方法一 System.Diagnostics.EventLog.WriteEntry("WebAppError", Message, System.Diagnostics.EventLogEntryType.Error); //寫入文字檔,方法二 System.IO.File.AppendAllText(Server.MapPath(string.Format("Log\\{0}.txt", DateTime.Now.Ticks.ToString())), Message); //寄出Email,方法三 //此方法請參考System.Net.Mail.MailMessage //清除Error Server.ClearError(); Response.Write("系統錯誤,請聯絡系統管理員!!"); } void Session_Start(object sender, EventArgs e) { // 啟動新工作階段時執行的程式碼 } void Session_End(object sender, EventArgs e) { // 工作階段結束時執行的程式碼。 // 注意: 只有在 Web.config 檔將 sessionstate 模式設定為 InProc 時, // 才會引發 Session_End 事件。如果將工作階段模式設定為 StateServer // 或 SQLServer,就不會引發這個事件。 } </script>
測試網頁,產生一個Null Exception protected void Page_Load(object sender, EventArgs e) { throw (new ArgumentNullException()); }
執行結果:
事件檢視器
文字檔
參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20080822113331G86&fumcde=FUM20041006161839LRJhttp://support.microsoft.com/kb/306355/zh-tw