Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Author: Sharad Pyakurel

Debugging and Tracing in ASP.NET


Debugging is the process of iden fying and resolving errors or defects in so ware applica ons,
or systems to ensure that they work as expected. The primary objec ve of debugging is to find
and fix errors, ensuring that the so ware operates correctly and efficiently.

1. Using Visual Studio Debugger: Visual Studio provides a comprehensive debugger for
ASP.NET applica ons. We can set breakpoints, inspect variables, step through code, and
analyze the flow of the program execu on.
To start the debugger, select F5, or choose the Debug Target bu on in the Standard
toolbar, or choose the Start Debugging bu on in the Debug toolbar, or choose Debug >
Start Debugging from the menu bar. We see the debugging information in console output.
To stop the debugger, select Shi +F5, or choose the Stop Debugging bu on in the Debug
toolbar, or choose Debug > Stop Debugging from the menu bar.
2. Using Breakpoints: Breakpoints are markers that we can set in our code to pause the
execu on at a specific line of the code. When the breakpoint is hit, we can examine the
current state of variables and step through the code line by line. Break points are set by
clicking on the margin on the le side of the code in the appropriate line. Debug execu on
will be stopped in the line where the breakpoint is defined. Further management of
execu on can be done by using the Step Into (F11), Step Over and Step Out keys.
3. Using Immediate Window: In Visual Studio, we can use the Immediate Window to
execute commands and evaluate expressions while debugging. This can help us to test
and analyze code in the run me.
4. Using Watch Window: The Watch Window allows us to monitor the values of variables
and expressions as we step through the code. We can add variables or expressions to the
Watch Window and see how their values change during execu on.

Tracing
Tracing is a debugging and diagnos c feature that is used to monitor the execu on flow of the
web applica on. It provides detailed informa on about the requests, events, and ac vi es
occurring during the execu on of a code. Tracing helps in iden fying performance bo lenecks,
errors, and other issues in the applica on. To enable tracing in an ASP.NET WebForms applica on,
we need to make some configura ons in web.config file.

1. Open web.config file.


Author: Sharad Pyakurel

2. Inside the <system.web> sec on, add or modify the <trace> element. Then, save the
web.config file.

<system.web>
<trace enabled="true" requestLimit="100" localOnly="false" pageOutput="true" />
</system.web>

 enabled: Specifies whether tracing is enabled (true to enable, false to disable).


 requestLimit: Limits the number of trace entries that are stored for a single request. We
can adjust this value as needed.
 localOnly: If set to true, tracing informa on will only be visible on the local machine. If
set to false, tracing can be viewed remotely as well.
 pageOutput: Specifies whether the trace informa on should be displayed at the bo om
of the page.

- If tracing is enabled, we will see trace informa on displayed at the bo om of the rendered
web page. This informa on will include data about the execu on of various events, the
me taken for each event, and other relevant details. Since tracing can expose sensi ve
informa on about the applica on it is not recommended to be enabled in a produc on
environment.
- We can also add custom trace messages in the code using the Trace.Write and Trace.Warn
methods.

Trace.Write("Connec onTrace", "Database connec on trace


Trace.Warn("Connec onTrace", "Database connec on trace.");
- We can Include Trace="true" in <%@ Page Title="" Language="C#"...%> direc ve to
enable tracing for the specific page only (i.e., Page level tracing).

<%@ Page Title="" Language="C#" MasterPageFile="~/MyProject.master"


AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="Pyprojects_Defailt "
Trace="true"%>

You might also like