asp.net页面出错时的处理方法
生活随笔
收集整理的這篇文章主要介紹了
asp.net页面出错时的处理方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.第一種做法,在Web.config文件配置
<system.web>??????<customErrors?defaultRedirect="~/ErrorPage.aspx"?
?????????????????????mode="RemoteOnly">
??????</customErrors>
</system.web>
?
? defaultRedirect屬性用來指明當(dāng)aspx頁面發(fā)生了未處理錯(cuò)誤時(shí)導(dǎo)向的頁面; 但Asp.net使用重定向機(jī)制來重新導(dǎo)航錯(cuò)誤頁面,這樣錯(cuò)誤信息就會(huì)丟失,也就是說我們用Server.GetLastError()獲得的Exception對(duì)象始終是空的。雖然可以提示用戶出錯(cuò),并提供一個(gè)返回出錯(cuò)頁面的鏈接,卻不能給管理員一個(gè)很好的錯(cuò)誤提示。
2.第二種做法:在global文件里的Application_Error方法中處理
代碼 protected?void?Application_Error(Object?sender,?EventArgs?e)????????{
????????????Exception?ex=Server.GetLastError().GetBaseException();
????????????string?errorTime="發(fā)生時(shí)間:"+DateTime.Now.ToString();
????????????string?errorAddress="發(fā)生異常頁:"+Request.Url.ToString();
????????????string?errorInfo="異常信息:"+ex.Message;
????????????string?errorSource="錯(cuò)誤源:"+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);//文件不在則創(chuàng)建,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"); //跳轉(zhuǎn)到顯示友好錯(cuò)誤的頁面
????????}
然后在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="發(fā)生時(shí)間:"+DateTime.Now.ToString();
????????????string?errorAddress="發(fā)生異常頁:"+Request.Url.ToString();
????????????string?errorInfo="異常信息:"+ex.Message;
????????????string?errorSource="錯(cuò)誤源:"+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);//文件不在則創(chuàng)建,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();//防止錯(cuò)誤繼續(xù)到要被處理的?Application_Error?事件中。
????????????Response.Redirect("~/ErrorPage.aspx");
????????????
????????}
?
我經(jīng)常的做法是使用第二種方法,然后再寫一個(gè)發(fā)送短信的方法(調(diào)用移動(dòng)的短信借口),這樣的話程序出錯(cuò)的時(shí)候,管理員可以收到程序出錯(cuò)的信息。
總結(jié)
以上是生活随笔為你收集整理的asp.net页面出错时的处理方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET点滴
- 下一篇: ASP.NET MVC:通过 FileR