protected void Application_Error(object sender, EventArgs e)
{
//在出現未處理的錯誤時運行的代碼
Exception objError = Server.GetLastError().GetBaseException();
string errortime = string.Empty;
string erroraddr = string.Empty;
string errorinfo = string.Empty;
string errorsource = string.Empty;
string errortrace = string.Empty;
string errormethodname = string.Empty;
string errorclassname = string.Empty;
errortime = "發生時間:" + System.DateTime.Now.ToString();
erroraddr = "發生異常頁: " + System.Web.HttpContext.Current.Request.Url.ToString();
errorinfo = "異常信息: " + objError.Message;
errorsource = "錯誤源:" + objError.Source;
errortrace = "堆棧信息:" + objError.StackTrace;
errorclassname = "發生錯誤的類名" + objError.TargetSite.DeclaringType.FullName;
errormethodname = "發生錯誤的方法名:" + objError.TargetSite.Name;
//清除當前異常 使之不返回到請求頁面
Server.ClearError();
lock (this)
{
//文件不存在就創建,true表示追加
// writer = new System.IO.StreamWriter(file.FullName, true);
#endregion
string ip = "用戶IP:" + Request.UserHostAddress;
string log = errortime + "##" + erroraddr + "##" + ip + "##" + errorclassname + "##" + errormethodname + "##" + errorinfo + "##" + errorsource + "##" + errortrace.Replace("\r\n", "<br>");
LogHelper.Error(new Exception(log), this.GetType().Name, "Global");
}
Response.Write("程序出現異常狀態,請聯系管理員");
}