1. The first method is to configure in the web. config file.
< System. Web >
< Customerrors defaultredirect = " ~ /Errorpage. aspx "
Mode = " Remoteonly " >
</ Customerrors >
</ System. Web >
The defaultredirect attribute is used to indicate the oriented page when an unhandled error occurs on the ASPX page; howeverAsp.net uses the redirection mechanism to re-navigate the error page, so that the error information will be lost. That is to say, the exception object obtained using server. getlasterror () is always empty. Although you can prompt the user for an error and provide a link to return the error page, you cannot give the Administrator a good error message.
2. Method 2: process in the application_error method of the Global File
Code
Protected Void Application_error (Object sender, eventargs E)
{
Exception ex = Server. getlasterror (). getbaseexception ();
String Errortime = " Occurrence time: " + Datetime. Now. tostring ();
String Erroraddress = " Exception page: " + Request. url. tostring ();
String Errorinfo = " Exception information: " + Ex. message;
String Errorsource = " Error Source: " + Ex. source;
String Errortrace = " Stack information: " + Ex. stacktrace;
Server. clearerror ();
System. Io. streamwriter writer = Null ;
Try
{
Lock ( This )
{
// Write logs
String Year = Datetime. Now. year. tostring ();
String Month = Datetime. Now. Month. tostring ();
String Day = Datetime. Now. Day. tostring ();
String Path = String . Empty;
String Filename = Datetime. Now. tostring ( " Yyyymmdd " ) + " . Txt " ;
Path = Server. mappath ( " ~ /Error/ " ) + Year + Month + Day;
If ( ! Directory. exists (PATH ))
{
Directory. createdirectory (PATH );
}
System. Io. fileinfo File = New Fileinfo (Path + " / " + Filename );
Writer = New Streamwriter (file. fullname, True ); // If the file is not present, it is created. True indicates appending.
Writer. writeline ( " User IP Address: " + request. userhostaddress);
writer. writeline (errortime);
writer. writeline (erroraddress);
writer. writeline (errorinfo);
writer. writeline (errorsource);
writer. writeline (errortrace);
writer. writeline ( " ------------------------------------------------------- " );
}
}
Finally
{
If (Writer ! = Null )
{
Writer. Close ();
}
}
Server. Transfer ( " ~ /Errorpage. aspx " ); // Hop Go to the page displaying friendly errors
}
Then, some friend prompts are displayed on the errorpage. ASPX page.
3. Method 3: handle in the page_error event
Code
Private Void Page_load ( Object Sender, system. eventargs E)
{
Throw ( New Argumentnullexception ());
}
Public VoidPage_error (ObjectSender, eventargs E)
{
Exception ex=Server. getlasterror (). getbaseexception ();
String Errortime = " Occurrence time: " + Datetime. Now. tostring ();
String Erroraddress = " Exception page: " + Request. url. tostring ();
String Errorinfo = " Exception information: " + Ex. message;
String Errorsource = " Error Source: " + Ex. source;
String Errortrace = " Stack information: " + Ex. stacktrace;
Server. clearerror ();
System. Io. streamwriter writer = Null ;
Try
{
Lock ( This )
{
// Write logs
String Year = Datetime. Now. year. tostring ();
String Month = Datetime. Now. Month. tostring ();
String Day = Datetime. Now. Day. tostring ();
String Path = String . Empty;
String Filename = Datetime. Now. tostring ( " Yyyymmdd " ) + " . Txt " ;
Path = Server. mappath ( " ~ /Error/ " ) + Year + Month + Day;
If ( ! Directory. exists (PATH ))
{
Directory. createdirectory (PATH );
}
System. Io. fileinfo File = New Fileinfo (Path + " / " + Filename );
Writer = New Streamwriter (file. fullname, True ); // If the file is not present, it is created. True indicates appending.
Writer. writeline ( " User IP Address: " + request. userhostaddress);
writer. writeline (errortime);
writer. writeline (erroraddress);
writer. writeline (errorinfo);
writer. writeline (errorsource);
writer. writeline (errortrace);
writer. writeline (" ------------------------------------------- " );
}
}
Finally
{
If (Writer ! = Null )
{
Writer. Close ();
}
}
Server. clearerror ();//Prevent errors from continuing to the application_error event to be processed.
Response. Redirect ("~ /Errorpage. aspx");
}
I often use the second method, and then write another method for sending text messages (calling the mobile text message excuse ).ProgramWhen an error occurs, the administrator can receive the program error message.