0% found this document useful (0 votes)
113 views24 pages

Page Level Tracing, Debugging, Error Handling (Example)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
113 views24 pages

Page Level Tracing, Debugging, Error Handling (Example)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

17/04/2019 Asp.

Net Page Level Tracing, Debugging, Error Handling [Example]


(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Asp.Net Page Level Tracing, Debugging, Error


Handling [Example]
▶ Scrum Master Certification In any application, errors are bound to occur
during the development process. It is important to
▶ SQL Server Performance Tuning be able to discover errors at an early stage.
▶ Machine Learning Training
In Visual Studio, it is possible to do this for ASP.Net
▶ Data Analytics Courses
applications. Visual Studio is used for Debugging
▶ AWS Certification Programs and has error handling techniques for ASP.Net.
▶ Data Science Courses
In this tutorial, you will learn-

What is Debugging in ASP.NET?


What is Tracing in ASP.NET?
Page Level Tracing
Error Handling: Displaying a Custom Error Page
ASP.NET Unhandled Exception
ASP.NET Error logging

What is Debugging in ASP.NET?


Debugging is the process of adding breakpoints to an application. These breakpoints are
used to pause the execution of a running program. This allows the developer to understand
what is happening in a program at a particular point in time.

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]

In the following example, we will see

How to make the demo application display a string.


How to add breakpoints to an application.
How to debug the application using this breakpoint.

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.

We are just adding the code line Response.Write to display a string.


So when the application executes, it should display the string "We are debugging" in the
web browser.

(/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.

Note: - You can add multiple breakpoints in an application

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.

What is Tracing in ASP.NET?


Application tracing allows one to see if any pages requested results in an error. When tracing
is enabled, an extra page called trace.axd is added to the application. (See image below).
This page is attached to the application. This page will show all the requests and their
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 4/24
status.
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]

PG Diploma in Data Science


Get: IIIT-B Certi ed, Alumni Status + upGrad's Career Assistance upGrad

(/images/asp-

net/061516_0956_AspNetTraci6.png)

Let's look at how to enable tracing for an application.

Step 1) Let's work on our 'DemoApplication'. Open the web.config file from the Solution
Explorer.

(/images/asp-net/061516_0956_AspNetTraci7.png)

Step 2) Add the below line of code to the Web.config file.

The trace statement is used to enable tracing for the application.

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)

<?xml version="1.0" encoding="utf-8"?>


<! --
For more information on how to configure your ASP.NET application, please visit http://go.m
icrosoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0” />

<trace enable="true" pageOutput="false" requestLimit="40" localOnly="fals


e"/>

</system.web>
</configuration>

Run the "demoapplication" in Visual Studio.

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

1. The time of the request for the web page.


2. The Name of the web page being requested.
3. The status code of the web request. (status code of 200 means that the request is
successful).
4. The View details which you allow to view more details about the web request. An
example of this is shown below. One important detailed information provided is the
header information. This information shows what is the information sent in the header of
each web request.

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)

Page Level Tracing


Page tracing shows all the general information about a web page when it is being processed.
This is useful in debugging if a page does not work for any reason.

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.

Let's look at how to enable tracing for an application at a page level.

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)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="DemoAppli


cation.Demo" %>

<!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.

Error Handling: Displaying a Custom Error Page


In ASP.Net, you can have custom error pages displayed to the users. If an application
contains any sort of error, a custom page will display this error to the user.

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.

Let's follow the below mentioned steps

Step 1) Let's work on our DemoApplication. Let's add an HTML page to the application

1. Right-click on the DemoApplication in Solution Explorer


2. Choose the menu option 'Add'->HTML Page
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 10/24
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]

(/images/asp-net/061516_0956_AspNetTraci14.png)

Step 2) In the next step, we need to provide a name to the new HTML page.

1. Provide the name as 'ErrorPage.'


2. Click the 'OK' button to proceed.

(/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” />

<customErrors mode="On" defaultRedirect="ErrorPage.html">


</customErrors>

</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)

Add the below code to the Demo.aspx.cs file.

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
{

public partial class Demo : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
String path = @"D:\Example.txt";
string[] lines;
lines = File.ReadAllLines(path);
}
}
}

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.

ASP.NET Unhandled Exception


Even in the best of scenarios, there can be cases of errors which are just not forseen.

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.

Let's see an example on this.

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
{

public partial class Demo : System.Web.UI.Page


{
protected void Application_Error(object sender, EventArgs e)
{
HttpException lastErrorWrapper = Server.GetLastError() as HttpExceptio
n;

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.

ASP.NET Error logging


By logging application errors, it helps the developer to debug and resolve the error at a later
point of time. ASP.Net has the facility to log errors. This is done in the Global.asax.cs file
when the error is captured. During the capturing process, the error message can be written
into a log file.

Let's see an example on this.

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
{

public partial class Demo : System.Web.UI.Page


{
protected void Application_Error(object sender, EventArgs e)
{

String str ="";


str = exc.Message;

String path = @"D:\AllErrors.txt";


File.WriteAllTest(path,str);
Server.trrasfer("~/ErrorPage.html");
}
}
}

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:-

Browse the page http://localhost:53003/Demo1.aspx . Remember that Demo1.aspx does not


exist in our application. You will then get the below 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

ASP.Net has the facility to perform debugging and Error handling.


Debugging can be achieved by adding breakpoints to the code. One then runs the Start
with Debugging option in Visual Studio to debug the code.
Tracing is the facility to provide more information while running the application. This can
be done at the application or page level.
At the page level, the code Trace=true needs to be added to the page directive.
https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 21/24
At the application level, an extra
17/04/2019
page called Trace.axd is created for the application. This
Asp.Net Page Level Tracing, Debugging, Error Handling [Example]

provides all the necessary tracing information.

 Prev (/insert-update-delete-asp-net.html) Report a Bug

Next  (/deploying-website-iis.html)

YOU MIGHT LIKE:

ASP.NET ASP.NET ASP.NET

(/asp-net-unit-testing- (/asp-net-controls- (/asp-net-first-


project.html) (/asp- webforms.html) program.html)
net-unit-testing- (/asp-net-controls- (/asp-net-first-
project.html) webforms.html) program.html)
UNIT TESTING in Asp.Net: ASP.NET Web Forms ASP.NET First Program
Complete Tutorial Tutorial: User Controls Example: Hello World
(/asp-net-unit-testing- Examples (/asp-net-first-
project.html) (/asp-net-controls- program.html)
webforms.html)

ASP.NET ASP.NET ASP.NET

(/deploying-website- (/asp-net-intro-life-cycle- (/asp-net-controls.html)


iis.html) hello.html) (/asp- (/asp-net-
(/deploying- net-intro-life-cycle- controls.html)
website-iis.html) hello.html) ASP.NET Controls:
How to Host a Website on ASP.NET Application & PAGE CheckBox, RadioButton,
IIS: Setup & Deploy Web Life Cycle ListBox, Textbox, Label
Application (/asp-net-intro-life-cycle- (/asp-net-controls.html)
(/deploying-website- hello.html)
iis.html)

ASP-Net. Tutorial
2) Application & PAGE Life Cycle (/asp-net-intro-life-cycle-hello.html)

3) ASP.NET First Program (/asp-net-first-program.html)

4) ASP.NET Controls (/asp-net-controls.html)

5) ASP.NET Session Management (/asp-net-session-management.html)


https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 22/24
g p g
17/04/2019 Asp.Net Page Level Tracing, Debugging, Error Handling [Example]
6) ASP.NET Web Forms (/asp-net-controls-webforms.html)

7) Insert, Update & Delete (/insert-update-delete-asp-net.html)

8) Tracing, Debugging, Error Handle (/asp-net-tracing-debugging-error-handling.html)

12) ASP.Net Interview Q & A (/asp-net-interview-questions-answers.html)

9) Deploying a website on IIS (/deploying-website-iis.html)

10) Unit Testing Project (/asp-net-unit-testing-project.html)

11) ASP.NET MVC (/asp-net-mvc-tutorial.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]

Execute Javascript (/execute-javascript-online.html)


Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2019


        Privacy Policy (/privacy-policy.html)

https://www.guru99.com/asp-net-tracing-debugging-error-handling.html 24/24

You might also like