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");
}
我經常的做法是使用第二種方法,然後再寫一個傳送簡訊的方法(調用移動的簡訊借口),這樣的話程式出錯的時候,管理員可以收到程式出錯的資訊。