Page Level Tracing, Debugging, Error Handling (Example)
Page Level Tracing, Debugging, Error Handling (Example)
Let's take an example of a program. The program displays a string "We are debugging" to
the user. Suppose when we run the application, for some reason, the string is not displayed.
To identify the problem we need to add a breakpoint. We can add a breakpoint to the code
line which displays the string. This breakpoint will pause the execution of the program. At
this point, the programmer can see what is possibly going wrong. The programmer rectifies
the program accordingly.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 1/24
Here in the example, we will useAsp.Net
17/04/2019
our 'DemoApplication' that was created in earlier chapters.
Page Level Tracing, Debugging, Error Handling [Example]
Step 1) Let's first ensure we have our web application open in Visual Studio. Ensure the
DemoApplication is open in Visual Studio.
(/images/asp-
net/061516_0956_AspNetTraci1.png)
Step 2) Now open the Demo.aspx.cs file and add the below code line.
(/images/asp-net/061516_0956_AspNetTraci2.png)
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 2/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
namespace DemoApplication
{
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("We are debugging");
}
}
}
Step 3) Now let's add a breakpoint. A breakpoint is a point in Visual Studio where you want
the execution of the program to stop.
(/images/asp-net/061516_0956_AspNetTraci3.png)
1. To add a breakpoint, you need to click the column where you want the breakpoint to be
inserted. So in our case, we want our program to stop at the code line "Response.Write".
You don't need to add any command to add a breakpoint. You just need to click on the
line on which you want to add a breakpoint.
2. Once this is done, you will notice that the code gets marked in red. Also, a red bubble
comes up in the column next to the code line.
Step 4) Now you need to run your application using Debugging Mode. In Visual Studio,
choose the menu option Debug->Start Debugging.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 3/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-
net/061516_0956_AspNetTraci4.png)
Output:-
(/images/asp-net/061516_0956_AspNetTraci5.png)
When you perform all the steps correctly, the execution of the program will break. Visual
Studio will go to the breakpoint and mark the line of code in yellow.
Now, if the programmer feels that the code is incorrect, the execution can be stopped. The
code can then be modified accordingly. To continue proceeding the program, the
programmer needs to click the F5 button on the keyboard.
(/images/asp-
net/061516_0956_AspNetTraci6.png)
Step 1) Let's work on our 'DemoApplication'. Open the web.config file from the Solution
Explorer.
(/images/asp-net/061516_0956_AspNetTraci7.png)
The 'requestLimit' in trace statement is used. It specifies the number of page requests
that has to be traced.
In our example, we are giving a limit of 40. We give limit because a higher value will
degrade the performance of the application.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 5/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci8.png)
</system.web>
</configuration>
Output:-
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 6/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci9.png)
If you now browse to the URL – http://localhost:53003/trace.axd , you will see the
information for each request. Here you can see if any errors occur in an application. The
following types of information are shown on the above page
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 7/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci10.png)
Visual Studio will provide detailed information about various aspects of the page.
Information such as the time for each method that is called in the web request. For example,
if your web application is having a performance issue, this information can help in
debugging the problem. This information is displayed when the application run's in Visual
Studio.
Step 1) Let's work on our DemoApplication. Open the demo.aspx file from the Solution
Explorer
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 8/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci11.png)
Step 2) Add the below line of code to enable page tracing. In the Page declaration, just
append the line Trace="true". This code line will allow page level tracing.
(/images/asp-net/061516_0956_AspNetTraci12.png)
<!DOCTYPE html>
<html xmlns="http://www.w3.ore/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server”>
</form>
</body>
</html>
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 9/24
Run the application in Visual Studio.
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
Output:-
(/images/asp-net/061516_0956_AspNetTraci13.png)
Now when the web page Demo.aspx is displayed, you will get a whole lot of information
about the page. Information such as the time for each aspect of the page lifecycle is
displayed on this page.
In our example, we are first going to add an HTML page. This page will display a string to the
user "We are looking into the problem". We will then add some error code to our demo.aspx
page so that the error page is shown.
Step 1) Let's work on our DemoApplication. Let's add an HTML page to the application
(/images/asp-net/061516_0956_AspNetTraci14.png)
Step 2) In the next step, we need to provide a name to the new HTML page.
(/images/asp-net/061516_0956_AspNetTraci15.png)
Step 3) The Errorpage will automatically open in Visual Studio. If you go to the Solution
Explorer, you will see the file added.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 11/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci16.png)
Add the code line "We are looking into the problem" to the HTML page. You don't need to
close the HTML file before making the change to the web.config file.
(/images/asp-net/061516_0956_AspNetTraci17.png)
<!DOCTYPE html>
<html xmlns="http://www.w3.ore/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
We are looking into the problem
</body>
</html>
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 12/24
Step 4) Now you need to make aAsp.Net
17/04/2019
change in the web.config file. This change will notify that
Page Level Tracing, Debugging, Error Handling [Example]
whenever an error occurs in the application, the custom error page needs to be displayed.
The 'customErrors' tag allows defining a custom error page. The defaultRedirect property is
set to the name of our custom error's page created in the previous step.
(/images/asp-net/061516_0956_AspNetTraci18.png)
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0” />
</system.web>
</configuration>
Step 5) Now let's add some faulty code to the demo.aspx.cs page. Open this page bydouble-
clickingg the file in Solution Explorer
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 13/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci19.png)
These lines of code are designed to read the lines of a text from a file.
The file is supposed to be located in the D drive with the name 'Example.txt.'
But in our situation, this file does not really exist. So this code will result in an error when
the application runs.
(/images/asp-net/061516_0956_AspNetTraci20.png)
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 14/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
namespace DemoApplication
{
Now execute the code in Visual Studio and you should get the below output.
Output:-
(/images/asp-
net/061516_0956_AspNetTraci21.png)
The above page shows that an error was triggered in the application. As a result, the
Error.html page is displayed to the user.
Suppose if a user browses to the wrong page in the application. This is something that
cannot be predicted. In such cases, ASP.Net can redirect the user to the errorpage.html.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 15/24
We are going to use our sameAsp.Net
17/04/2019
'DemoApplication' which has the Errorpage.html.
Page Level Tracing, Debugging, Error Handling [Example]
And we will try to view a web page which does not exist in our application.
We should be redirected to our ErrorPage.html page in this case. Let's see the steps to
achieve this.
Step 1) Let's work on our DemoApplication. Open the Global.asax.cs file from the Solution
Explorer
(/images/asp-net/061516_0956_AspNetTraci22.png)
NOTE: The global.asax.cs file is used to add code that will be applicable throughout all
pages in the application.
Step 2) Add the below line of code to the global.asax.cs. These lines will be used to check for
errors and display the ErrorPage.html page accordingly.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 16/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci23.png)
namespace DemoApplication
{
if(lastErrorWrapper.GetHttpCode() == 404)
Server.T ransfer("~/ErrorPage.html");
}
}
}
Code Explanation:-
1. The first line is the Application_Error event handler. This event is called whenever an
error occurs in an application. Note that the event name has to be 'Application_Error'.
And the parameters should be as shown above.
2. Next, we define an object of the class type HttpException. This is a standard object which
will hold all the details of the error. We then use the Server.GetLastError method to get all
the details of the last error which occurred in the application.
3. We then check if the error code of the last error is 404. (The error code 404 is the standard
code returned when a user browses to a page which is not found). We then transfer the
user to the ErrorPage.html page if the error code matches.
Now run the code in Visual Studio and you should get the below output
Output:-
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 17/24
Browse the page http://localhost:53003/Demo1.aspx
17/04/2019
. Remember that Demo1.aspx does not
Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
exist in our application. You will then get the below output.
(/images/asp-
net/061516_0956_AspNetTraci24.png)
The above page shows that an error was triggered in the application. As a result, the
Error.html page is displayed to the user.
We are going to use our same DemoApplication which has the Errorpage.html.
And we will try to view a web page which does not exist in our application.
We should be redirected to our ErrorPage.html page in this case.
And at the same time, we will write the error message to a log file. Let's see the steps to
achieve this.
Step 1) Let's work on our DemoApplication. Open the Global.asax.cs file from the Solution
Explorer
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 18/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-net/061516_0956_AspNetTraci25.png)
Step 2) Add the below line of code to the global.asax.cs. It will check for errors and display
the ErrorPage.html page accordingly. Also at the same time, we will log the error details in a
file called 'AllErrors.txt.' For our example, we will write code to have this file created on the D
drive.
(/images/asp-net/061516_0956_AspNetTraci26.png)
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 19/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
namespace DemoApplication
{
Code Explanation:-
1. The first line is to get the error itself by using the 'Server.GetLastError' method. This is
then assigned to the variable 'exc'.
2. We then create an empty string variable called 'str'. We get the actual error message
using the 'exc.Message' property. The exc.Message property will have the exact message
for any error which occurs when running the application. This is then assigned to the
string variable.
3. Next, we define the file called 'AllErrrors.txt.' This is where all the error messages will be
sent. We write the string 'str' which contains all the error messages to this file.
4. Finally, we transfer the user to the ErrorPage.html file.
Output:-
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 20/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
(/images/asp-
net/061516_0956_AspNetTraci27.png)
And at the same time, if you open the 'AllErrors.txt' file you will see the below information.
(/images/asp-net/061516_0956_AspNetTraci28.png)
The error message can then be passed on to the developer at a later point in time for
debugging purposes.
Summary
Next (/deploying-website-iis.html)
ASP-Net. Tutorial
2) Application & PAGE Life Cycle (/asp-net-intro-life-cycle-hello.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)
Interesting
Books to Read! (/books.html)
Blog (/blog/)
Quiz (/tests.html)
eBook (/ebook-pdf.html)
Execute online
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 23/24
17/04/2019
Execute JavaAsp.Net
Online (/try-java-editor.html)
Page Level Tracing, Debugging, Error Handling [Example]
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 24/24