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 redirected page when an unhandled error occurs on the ASPX page. However, Asp.net uses the redirection mechanism to re-navigate the error page, so that the error information will be lost, that is, we use server. the exception object obtained by getlasterror () is always null. 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:" + 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 "); // jump 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 void page_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:" + 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 the error from continuing to the application_error event to be processed.
Response. Redirect ("~ /Errorpage. aspx ");
}
My usual practice is to use the second method, and then write a method for sending text messages (calling mobile text message excuse). In this case, when a program error occurs, the administrator can receive the program error message.