There are three error modes in which an ASP.Net application can work:
1.) Off Mode
2.) On Mode
3.) RemoteOnly Mode
The Error mode attribute determines whether or not an ASP.Net error message is displayed. By default, the mode value is set to "RemoteOnly".
Off Mode
When the error attribute is set to "Off", ASP.Net uses its default error page for both local and remote users in case of an error.
On Mode
In case of "On" Mode, ASP.Net uses user-defined custom error page instead of its default error page for both local and remote users. If a custom error page is not specified, ASP.Net shows the error page describing how to enable remote viewing of errors.
RemoteOnly
ASP.Net error page is shown only to local users. Remote requests will first check the configuration settings for the custom error page or finally show an IIS error.
Customization of error page can be implemented by adding a value for an attribute "defaultRedirect" in the
Notification of Error to the Administrator
In a practical web application, customization of error pages is not the only requirement. The error, if encountered, should be reported to the administrator so that it can be rectified thus enabling subsequent requests to work properly without any error.
Notification of the error can be sent to the administrator in one of the following two ways:
1.) Error can be registered as a log entry in the Windows Event Log on the administrator's machine.
2.) An Email can be sent to the administrator with a suitable error message.
Writing to the Event Log
In ASP.Net, error can be handled programmatically by writing appropriate code in the page-level error event, for errors on an individual page or in the application-level error event for handling errors that may occur in any page of the application.
Therefore, code for writing in the Event Log should be written in either of the events, depending on the requirement of the application. To illustrate this example, I have written the code in the application-level event with the error mode set to "RemoteOnly" and the "defaultRedirect" attribute to "error.htm". The application-level error event should be included in the global file "global.asax" within the same application folder.
Sending Email To the Administrator
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Mail
Public Class Global
Inherits System.Web.HttpApplication
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim mail As New MailMessage
Dim ErrorMessage = "The error description is as follows : "
& Server.GetLastError.ToStringmail.To = administrator@domain.com
mail.Subject = "Error in the Site"
mail.Priority = MailPriority.High
mail.BodyFormat = MailFormat.Text
mail.Body = ErrorMessage
SmtpMail.Send(mail)
End Sub
End Class
Monday, December 3, 2007
Custom Error Handling in ASP.NET
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment