|
Debugging ASP.NET Pages
Errors in programming are inevitable. Everyone of us introduce errors while coding.
Errors are normally referred to as bugs. The process of going through the code to
identify the cause of errors and fixing them is called Debugging.
There are three types of errors: syntax errors, run time errors, logical errors.
Error Handling
ASP.NET provides support for handling and tracking errors that might occur while developing
and testing applications. When we run an ASP.NET application, if an error occurs on
the server that processes the page, an HTML error page is generated and displayed
to the user in the browser. While error messages are displayed, ASP.NET takes care
of the security issues and makes sure that no crucial information is displayed in
the browser to the user. Crucial information can be in the form of compiler messages,
configuration settings, filenames, stack traces and source code. When an error occurs,
an error message is displayed to the user. To see the error details one of the following
should be done: Access the page again from the server or modify the configuartion
settings in the Web.Config file.
Creating Custom Error Pages
As mentioned above, when an error occurs with an ASP.NET application, an HTML
error page is displayed to the user. You can also create your own error page
and display that to the user instead of the HTML error page that is generated. The
page which you create is called the Custom error page. To create your own custom error
page, open a Web page and insert some text in it or an image or both. The custom error
page can be an html page or an aspx page. Once you are finsihed designing your error
page you need to modify the Web.Config file of the application so that it redirects
the user to the error page created in case an error occurs. The configuration file
should be as follows:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" DefaultRedirect="Error.aspx"/>
'assuming your error page is error.aspx
</system.web>
</configuration>
|
The mode attribute of the customErrors tag can be set to three values which are as
follows:
On: Custom error messages are always sent to the user and
the detailed ASP.NET error page is not shown
Off: Only original messages are sent to the user even if
a custom error page is available
RemoteOnly: Custom error messages are displayed only to
remote users accessing the site. If no custom error page is present then the remote
users see a message indicating that an error has occured
Setting Status Code Errors
While browsing the Internet you might have come across these kind of error messages,
Page not Found, You are Not Authorized to View this page, HTTP Error 404 - File or
directory not found, etc. This type of errors are called as status code errors
and you can set these kind of errors in the ASP.NET Web.Config file. The type
of error will be identified by the HTTP status code. The Web.Config file for that
looks like this:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" DefaultRedirect="http://startvbdotnet.com/Error.aspx"/>
<error statuscode="404" redirect="http://startvbdotnet.com/errorpages/notfound.aspx"/>
<error statuscode="403"_
redirect="http://startvbdotnet.com/errorpages/notauthorized.aspx"/>
</customErrors>
</system.web>
</configuration>
|
Writing Errors to Event Log
In the above paragraphs you were explained how to display custom error pages to users
when an error occurs. In addition to displaying errors messages to users you should
also be able to track errors so that you can identify and solve the problems associated
with your code. For that, you need to implement error tracking. Error Tracking can
be implemented by the Application_Error event (in Global.asax file), an application-level
event that is generated when an error occurs during the processing of the Web request.
The errors can then be tracked by using the Event Log, writing to database, etc.
To write to the NT Event Log you should add code to the Application_Error event handler
in the Global.asax.vb file of your application. The code should be as follows:
Imports System.Diagnostics
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim pageUrl, message, logname As String
Dim eventlog As New EventLog()
pageUrl = Request.Path
'requesting page URL that has the error
message = "Page Url is:" & pageUrl
'creating error message to write to eventlog
logname = "Application log"
'specifying event log name
If (Not eventlog.SourceExists(logname)) Then
eventlog.CreateEventSource(logname, logname)
'creating event log
End If
eventlog.Source = logname
eventlog.WriteEntry("Application error is" + message, EventLogEntryType.Error)
'writing to log
End Sub
|
Whenever an execption is generated with your application that exception will fire
the Application_Error event in the Global.asax file which will record a log of the
error in the Windows Event Log viewer.
|