asp.net页面出错时的处理方法
生活随笔
收集整理的這篇文章主要介紹了
asp.net页面出错时的处理方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.第一種做法,在Web.config文件配置
<system.web>??????<customErrors?defaultRedirect="~/ErrorPage.aspx"?
?????????????????????mode="RemoteOnly">
??????</customErrors>
</system.web>
?
? defaultRedirect屬性用來指明當aspx頁面發生了未處理錯誤時導向的頁面; 但Asp.net使用重定向機制來重新導航錯誤頁面,這樣錯誤信息就會丟失,也就是說我們用Server.GetLastError()獲得的Exception對象始終是空的。雖然可以提示用戶出錯,并提供一個返回出錯頁面的鏈接,卻不能給管理員一個很好的錯誤提示。
2.第二種做法:在global文件里的Application_Error方法中處理
代碼 protected?void?Application_Error(Object?sender,?EventArgs?e)????????{
????????????Exception?ex=Server.GetLastError().GetBaseException();
????????????string?errorTime="發生時間:"+DateTime.Now.ToString();
????????????string?errorAddress="發生異常頁:"+Request.Url.ToString();
????????????string?errorInfo="異常信息:"+ex.Message;
????????????string?errorSource="錯誤源:"+ex.Source;
????????????string?errorTrace="堆棧信息:"+ex.StackTrace;
????????????Server.ClearError();
????????????System.IO.StreamWriter?writer=null;
????????????try
????????????{
????????????????lock(this)
????????????????{
????????????????????//寫入日志?
????????????????????string?year=DateTime.Now.Year.ToString();
????????????????????string?month=DateTime.Now.Month.ToString();
????????????????????string?day=DateTime.Now.Day.ToString();
????????????????????string?path=string.Empty;
????????????????????string?filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
????????????????????path=Server.MapPath("~/Error/")+year+month+day;
????????????????????if(!Directory.Exists(path))
????????????????????{
????????????????????????Directory.CreateDirectory(path);
????????????????????}
????????????????????System.IO.FileInfo?file=new?FileInfo(path+"/"+filename);
????????????????????writer=new?StreamWriter(file.FullName,true);//文件不在則創建,true表示追加
????????????????????writer.WriteLine("用戶IP:"+Request.UserHostAddress);
????????????????????writer.WriteLine(errorTime);
????????????????????writer.WriteLine(errorAddress);
????????????????????writer.WriteLine(errorInfo);
????????????????????writer.WriteLine(errorSource);
????????????????????writer.WriteLine(errorTrace);
????????????????????writer.WriteLine("-------------------------------------------------------");
????????????????}
????????????}
????????????finally
????????????{
????????????????if(writer!=null)
????????????????{
????????????????????writer.Close();
????????????????}
????????????}
????????????Server.Transfer("~/ErrorPage.aspx"); //跳轉到顯示友好錯誤的頁面
????????}
然后在ErrorPage.aspx頁面顯示一些好友的提示信息.
3.第三種做法:在Page_Error事件里面處理
代碼 ????????private?void?Page_Load(object?sender,?System.EventArgs?e)????????{
????????????throw(new?ArgumentNullException());
????????}
????????public?void?Page_Error(object?sender,EventArgs?e)
????????{
????????????Exception?ex=Server.GetLastError().GetBaseException();
????????????string?errorTime="發生時間:"+DateTime.Now.ToString();
????????????string?errorAddress="發生異常頁:"+Request.Url.ToString();
????????????string?errorInfo="異常信息:"+ex.Message;
????????????string?errorSource="錯誤源:"+ex.Source;
????????????string?errorTrace="堆棧信息:"+ex.StackTrace;
????????????Server.ClearError();
????????????System.IO.StreamWriter?writer=null;
????????????try
????????????{
????????????????lock(this)
????????????????{
????????????????????//寫入日志?
????????????????????string?year=DateTime.Now.Year.ToString();
????????????????????string?month=DateTime.Now.Month.ToString();
????????????????????string?day=DateTime.Now.Day.ToString();
????????????????????string?path=string.Empty;
????????????????????string?filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
????????????????????path=Server.MapPath("~/Error/")+year+month+day;
????????????????????if(!Directory.Exists(path))
????????????????????{
????????????????????????Directory.CreateDirectory(path);
????????????????????}
????????????????????System.IO.FileInfo?file=new?FileInfo(path+"/"+filename);
????????????????????writer=new?StreamWriter(file.FullName,true);//文件不在則創建,true表示追加
????????????????????writer.WriteLine("用戶IP:"+Request.UserHostAddress);
????????????????????writer.WriteLine(errorTime);
????????????????????writer.WriteLine(errorAddress);
????????????????????writer.WriteLine(errorInfo);
????????????????????writer.WriteLine(errorSource);
????????????????????writer.WriteLine(errorTrace);
????????????????????writer.WriteLine("-------------------------------------------");
????????????????}
????????????}
????????????finally
????????????{
????????????????if(writer!=null)
????????????????{
????????????????????writer.Close();
????????????????}
????????????}
????????????Server.ClearError();//防止錯誤繼續到要被處理的?Application_Error?事件中。
????????????Response.Redirect("~/ErrorPage.aspx");
????????????
????????}
?
我經常的做法是使用第二種方法,然后再寫一個發送短信的方法(調用移動的短信借口),這樣的話程序出錯的時候,管理員可以收到程序出錯的信息。
總結
以上是生活随笔為你收集整理的asp.net页面出错时的处理方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET点滴
- 下一篇: ASP.NET MVC:通过 FileR