• No results found

Error Handling

In document ASP Bible pdf (Page 116-118)

ASP.NET provides rich support for handling and tracking errors that might occur while applications are running. When you run an ASP.NET application, if an error occurs on a server, an HTML error page is generated and displayed in the browser. While displaying error messages to users, ASP.NET takes care of the security issues by default, which makes it a reliable development tool for Web applications. ASP.NET ensures that no secure information, such as the remote machine compiler messages, configuration settings, filenames, stack traces, or source code, is revealed on the client

machine. When an error occurs, a generic error message, "Application Error Occurred," is displayed to users. To see the error details, one of the following needs to be done:

§ Access the page again from the local server. § Modify the configuration settings of the computer.

§ Modify the configuration settings of the application's Web.config file to enable remote access.

Following is a sample of the Web.config file that you can modify: <configuration>

<system.web>

<customErrors mode="Off" /> </system.web>

</configuration>

In this code, the <customErrors> tag has an attribute mode whose value is set to

"Off". This value indicates that the remote users always see the original error message that is generated on the server.

Using custom error pages

As mentioned earlier, an HTML error page is displayed to a user in case an error occurs on a server. These error messages are secure, because they do not leak any secret information. However, these pages are not pretty to see. You can create custom error pages that can be displayed in case errors occur. For example, you can create an error page that displays the company's brand and some error messages that you want to display. To implement the custom error pages:

1. Create a Web page that you want to display as an error message page. This can be a page with an .html or .aspx extension.

2. Modify the Web.config file of your application to point to the custom page in the event of any error. The configuration settings, shown here, point to a file called MyError.aspx:

3. <configuration> 4. <system.web> 5. <customErrors mode="RemoteOnly" defaultRedirect="MyError.aspx"/> 6. 7. </system.web> </configuration>

The mode attribute of the <customErrors> tag can take three values:

§ On: Indicates that the custom error messages are always sent to users and that the detailed ASP.NET error page is never shown.

§ Off: Indicates that only original error messages are sent to users even if a custom error page is available.

§ RemoteOnly: Indicates that the custom error messages are displayed only to remote users accessing the site. If no custom error page is

available, remote users simply see a message indicating that an error has occurred.

When you modify the Web.config file to set the defaultRedirect attribute, the user is directed to the same custom error message irrespective of the type of the error. The type of the error is identified by the HTTP status code. You can specify specific error

messages, such as "Page not found" or "server crash" for specific status codes, as shown in the following code:

<configuration> <system.web> <customErrors defaultRedirect="http://host1/MyError.aspx" mode="RemoteOnly"> <error statusCode="500" redirect="http://host1/pages/support.html"/> <error statusCode="403" redirect="http://host1/pages/access_denied.html"/> </customErrors> </system.web> </configuration>

In this code, the error tag takes two attributes, statusCode and redirect. The

statusCode attribute represents the value of the HTTP status code. The redirect

attribute points to the error message file. Tracking errors

In an earlier section, you saw how to display custom error messages to users in case errors occur on a server. In addition to displaying error messages to users, you should ensure that the administrators and developers are also able to track errors. This would allow them to identify and solve the problems associated with the code.

You can implement error tracking by handling the Application_Error event, an application-level event that is generated when an exception occurs during the processing of a Web request. The developers can use this event handler to obtain information, such as the page URL, the query string arguments, and cookie values. With this information, developers can write the code to track the problem or notify administrators and

developers about the problem. The errors can be tracked by using the Event Log, sending e-mail to administrators, or writing to a database etc.

You can write to the NT Event Log by adding code in the Application_Error event handler in the Global.asax file of your application. You can write to the NT Event Log only after you've imported the System.Diagnostics namespace.

To implement error tracking, create a Visual Basic ASP.NET Web Application project. In this project, import the System.Diagnostics namespace in the Global.asax file:

Imports System.Diagnostics

Then, write the following code in the event handler for the Application_Error event (in the Global.asax file). This code demonstrates how to write an Event Log to track errors related to page URL. The Event Log is named MyLog.

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an error occurs

'Retrieving the request URL

Dim pageUrl, message, logName as String Dim event_log1 as New EventLog

pageUrl = Request.Path

'Creating error message to write to Event Log message = "Page URL: " & pageUrl

'Specifying the Event Log name logName = "MyLog"

'Creating Event Log if it does not exist

If (Not EventLog.SourceExists (logName)) Then EventLog.CreateEventSource (logName,logName) End If

'Writing to the log

event_log1.Source = logName

event_log1.WriteEntry ("Application error occured. " + message,EventLogEntryType.Error)

End Sub

The application project contains a default Web Form, WebForm1.aspx. Rename this file to read "MakeEvent.aspx." Open the code behind file of this page and write the following statement to import the System.IO namespace:

Imports System.IO

Finally, in the Page_Load method, write the following code:

Sub Page_Load(Sender As System.Object, e As System.EventArgs) Handles MyBase.Load

'Cause the event log entry to be made

throw New FileNotFoundException("test exception") End Sub

When the MakeEvent.aspx page is executed, it generates an exception called

FileNotFoundException. This exception will fire the Application_Error event in the Global.asax file, which in turn will record a log of this error in the Windows Event Log. You will be able to see the errors using the Windows Event Log Viewer. To do so:

1. Select Start → Programs → Administrative Tools → Event Viewer. 2. Click the Event Log node to open the log. In this case, the name of the

log is MyLog. You need to select the log file from the

\winnt\system32\config folder. The file will be named MyLog.evt.

In document ASP Bible pdf (Page 116-118)