asp.net uses Global.asax to capture exception errors in the entire solution

            I saw a post today about using the global application class to help get exception information, using server.Transfer('''') to specify the page that accepts errors; plus using server.GetLastError() in the accepting error page to get The previous exception source.

 

            The Application_Error function in Global.asax is as follows:

           

[c-sharp]  
 
  1. protected void Application_Error(object sender, EventArgs e)  
  2.        {  
  3.            // Catch all exceptions under the entire solution  
  4.            try  
  5.            {  
  6.                Server.Transfer("~/Error.aspx");  
  7.            }  
  8.            catch { }  
  9.        }   

            The relevant code of the error acceptance page Error.aspx to obtain the exception information is as follows: 

[c-sharp]  
  1. Exception ex = Server.GetLastError().GetBaseException();  //Get exception source  
  2.                if (ex != null)  
  3.                {    
  4.                    Response.Write(ex.Message);      
  5.                }  
  6.                // clear the previous exception  
  7.                Server.ClearError();   

            The test exception code in the test page Text.aspx is as follows:

           

[c-sharp]   
  1. //Test whether the exception information is caught  
  2.    //test1  
  3.   //int UserID = Convert.ToInt32(Request["UserID"].ToString());  
  4.   
  5.   
  6.   // test2  
  7.   string Name = "aganar";  
  8.   
  9.   int UID = Convert.ToInt32(Name);  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325233986&siteId=291194637