Asp.net page error handling method summary

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.