C#全域性錯誤捕捉
protected void Application_Error(Object sender, EventArgs e) { Exception lastError = Server.GetLastError(); if (lastError != null) { //異常資訊 string strExceptionMessage = string.Empty; try { string url = Request.Url.OriginalString; string[] ignorFiles = { ".css", ".js", ".jpg", ".jpeg", ".png", ".rar", ".exe", ".bmp", ".png", ".doc", ".docx", ".xml", ".xsd", ".xls", ".xlsx", ".ppt", ".pptx" }; //如果是忽略檔案,不寫入訪問日誌 if (ignorFiles.Any(m => url.EndsWith(m, StringComparison.OrdinalIgnoreCase))) { return; } } catch { } //對HTTP 404做額外處理,其他錯誤全部當成500伺服器錯誤 HttpException httpError = lastError as HttpException; //if (httpError != null) //{ // //獲取錯誤程式碼 // int httpCode = httpError.GetHttpCode(); // strExceptionMessage = httpError.Message; // if (httpCode == 400 || httpCode == 404) // { // Response.StatusCode = 404; // Server.ClearError(); // //Response.Redirect("~/Web/Error?title=" + Server.UrlEncode("404") + "&desc=" + Server.UrlEncode("請求的資源不存在!")); // Response.Redirect("~/Base/Error?note=無效的URL路徑!"); // return; // } //}
strExceptionMessage = lastError.Message;
/*----------------------------------------------------- * 此處程式碼可根據需求進行日誌記錄,或者處理其他業務流程 * ---------------------------------------------------*/
/* * 跳轉到指定的http 500錯誤資訊頁面 * 跳轉到靜態頁面一定要用Response.WriteFile方法