ASP.NET Global捕獲異常

#region 捕獲異常
        /// <summary>
        /// 出錯處理:寫日誌,導航到公共出錯頁面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            if (Server.GetLastError() == null) return;
            Exception ex = Server.GetLastError().GetBaseException();
            string error = this.DealException(ex);
            LogHelper.LogInfo(error);
            if (ex.InnerException != null)
            {
                error = this.DealException(ex);
                LogHelper.LogInfo("異常捕獲時間:" + DateTime.Now.ToString() + "\n" + error);
            }
            this.Server.ClearError();
            this.Response.Redirect("UserLogin2.aspx");
        }
        /// <summary>
        /// 處理異常,用來將主要異常信息寫入文本日誌
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private string DealException(Exception ex)
        {
            this.Application["StackTrace"] = ex.StackTrace;
            this.Application["MessageError"] = ex.Message;
            this.Application["SourceError"] = ex.Source;
            this.Application["TargetSite"] = ex.TargetSite.ToString();
            string error = string.Format("URl:{0}\n引發異常的方法:{1}\n錯誤信息:{2}\n錯誤堆棧:{3}\n",
              this.Request.RawUrl, ex.TargetSite, ex.Message, ex.StackTrace);
            return error;
        }
        #endregion

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章