When a server error occurs in Asp.net, the specified error page is displayed, and the error information is written to the system log file.

Source: Internet
Author: User
1. Enter the page displayed when an error occurs in Web. config. Different error pages can be displayed based on different statuscode.

Code

<Customerrors mode = "on" defaultredirect = "/error/customerrorpage. aspx"> // off
<Error statuscode = "404" Redirect = "/error/404page. aspx"/>
<Error statuscode = "403" Redirect = "/error/403page. aspx"/>
</Customerrors>

 

2. Add the application error code to the Global. asax file and write it to the system log file.

Code

Protected void application_error (Object sender, eventargs E)
{
Exception lasterror = server. getlasterror ();
String errmessage = lasterror. tostring ();
String LogName = "MyLog ";
String Message = "Url" + Request. Path + "Error:" + ErrMessage;
// Create a log event name
If (! EventLog. SourceExists (LogName ))
{
EventLog. CreateEventSource (LogName, LogName );
}
EventLog Log = new EventLog ();
Log. Source = LogName; // Log name, which will be displayed in the Event Viewer
// The following are 5 events
Log. WriteEntry (Message, EventLogEntryType. Information, 1 );
Log. WriteEntry (Message, EventLogEntryType. Error, 2 );
Log. WriteEntry (Message, EventLogEntryType. Warning, 3 );
Log. writeentry (message, eventlogentrytype. successaudit, 4 );
Log. writeentry (message, eventlogentrytype. failureaudit, 5 );
}

 

3. An error is generated in default. aspx. cs. The default error page is displayed! Code

Private void page_load (Object sender, system. eventargs E)
{
Try
{
Int y = 0;
Int x = 1/y;
}
Catch (exception ERR)
{
Throw new exception ("404"); // I want to generate different errors, corresponding to statuscode in Web. config.
}
}

 

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.