c# .net 我的Application_Error 全局異常抓取處理


  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("程序出現異常狀態,請聯系管理員");
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM