Search Box

Friday 22 July 2011

ERROR HANDLING


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.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 applications 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:
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.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:
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.
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:
<configuration>
<system.web>
<customErrors mode=”RemoteOnly”defaultRedirect=”MyError.aspx”/>
</system.web>
</configuration>
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.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 codes.The redirect attribute points to the errors message file.
The ASP.Net Trace functionality:
In ASP.Net ,the trace feature ensures that the programmers are able to log their applications by providing the means to monitor and examine program performance either during development or after deployment.ASP.Net allows tracing at two levels:
Page-level tracing
Application-level tracing
Page-level Tracing:
ASP.Net makes it easy to debug and test applications by providing a trace capability.Trace capability is enabled,ASP.Net provides the following functionalities automatically:
Creates and appends a table called the tables of  performance data to the end of the ASP.Net page.
Allows a developer to add custom diagnostic messages in the code wherever required.
Basically,the following are the two ways to generate trace statements in a page:
1.Use the code written within a file
2.Use an HTML editor.
While generating the trace statements,you include custom trace messages to the Trace log.Then with the help of an using HTML editor,you can present those messages and other trace information in a better manner.
You’ll now write an ASP.Net page that generates the trace statements.Both Visual Studio.Net and Notepad can be used for writing the code.In this case,Notepad is used to create the ASPX file.
Open Notepad and type the following code:


<%@ Page Language=”VB” Trace=”False”%>
<html>
<head>
<title>Trace Demo</title>
</head>

<Script runat=”server”>
Public Function Addition(FNum As Integer,SNum As Integer)
As Integer
Trace.Write(“Inside Addition()FNum:”,FNum.ToString())
Trace.Warn(“Inside Addition()SNum:”,SNum.ToString())
Return FNum+SNum
End Function
</Script>

<body>
Calling the Addition Function:10+5=<%=Addition(10,5)%>
</body>
</html>

Save the file as an ASPX file in a Web directory on the Web server.In this case,the file is named TraceStat.aspx.Excute the TraceStat.aspx file.The Trace.Write statements generate the trace statements.The Addition function takes two integer values and returns an integer value as the sum of the two numbers.In the calling statement,the Addition functions is called by using the <% and the %> delimiters used for specifying the ASP code.
Application-level tracing:
Applicaation-level Tracing is enabled by using the Web.config file. This file is also used to enable the ASP.NET framework to gather the HTTP request information for the entire application. Application-level Tracing does not present the Trace information in a browser, but the information can be displayed in a webbased Traceviewer application.Trace viewer displays trace information for a sequence of requests to the application thus making it mandatory to store the matrix for each request in memory until tracing is enebled. This can be done by including a TraceContex class that participates in the HTTP execution.By opening the root web.config file and looking at the tracing section,the following code can be seen:
<configuration>
<system.web>
<trace enabled=”false”requestLimit=”10” pageOutput=”false”
traceMode=”SortBy Time’/>
</system.web>
</configuration>

No comments:

Post a Comment