ASP.NET利用Global.asax的Application_Error来记录Exception信息 
记录网页Exception讯息的方法有很多种.在此介绍两种储存方式.
1.记录在事件检视器里
2.记录在文字文件里
代码 
 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("系統錯誤,請聯絡系統管理員!!");  
 }
- 本文标题: ASP.NET利用Global.asax的Application_Error来记录错误日志
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
- 上一篇:鼠标经过UL上的li时更换背景色
- 下一篇: Asp.net网站速度性能优化总结