asp.net to display the specified error page when the server is in error and write the error message to the system log file

Source: Internet
Author: User
Tags config error code log system log
Asp.net| Error | server | show | page asp.net display the specified error page when the server is in error write the error message into the system log file

First, in the web.config to fill out the error displayed in the page, can be based on different statuscode display different error page.
<customerrors mode= "on"//if set to OFF, an error returns only the error message and does not jump to its own specified page defaultredirect= "/error/customerrorpage.aspx" >
<error statuscode= "404" redirect= "/error/404page.aspx"/>
<error statuscode= "403" redirect= "/error/403page.aspx"/>
</customErrors>

Second, add the application error code to the Global.asax file and write to the system log file
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 Event Log If It doesn ' t Exist

if (! EventLog.SourceExists (LogName))
{
EventLog.CreateEventSource (LogName, LogName);
}
EventLog Log = new EventLog ();
Log.source = LogName;
These are the five options that would display a different icon.
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);

}
Three, now you can do the test.
I created a mistake in the Default.aspx.cs, and sure enough I jumped to the default error page!
private void Page_Load (object sender, System.EventArgs e)
{
Put user code to initialize the page here
Try
{
int y=0;
int x=1/y;
}
catch (Exception ERR)
{
throw new Exception ("404");//I want to produce a different error, corresponding to the statuscode in Web.config, how to achieve?
Err.
}




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.