There are two ways to debug web form on asp.net page.
1. Debug object
2. Trace object
For debug object, it requires to compile the code in debug mode with namespace System.Diagnostics. Debug object will not function if it is not compiled in debug mode.
For Trace object, you are able to disable it by configuring web.config file. This can avoid the efforts to remove all trace objects on production server.
Trace object can be enabled in two levels:
1. Page level
2. Application level
For Page level, we could do it on Page attribute in .aspx with Trace=”true” ,the trace message can display on the page.
For Application level, we could do it on web.config file. This will enable the trace on every page in the application. The trace message can be displayed on the page or memory. You will see the following code on web.config file.
<configuration><system.web>
<trace enabled =”true” pageOutput=”true” localOnly=”true”/>
</system.web></configuration>
attribute pageOutput indicates if there is any output shown on the page. If it is false, the trace is kept in the memory, it is required to use http://servername / projName / trace.axd to view the trace information in the memory.This will create a security risks as the trace information is very sensitive to an application. It is possible to turn off the trace in memory with the following code in machine.config in WIN \ Microsoft.NET\Framework\version No.\Config
<httpHandlers> <add verb=”*” path = ” ” type = ” …….. > </httpHandlers>
The default value for path is trace.axd.
attribute localOnly indicates if the trace is shown on the local computer instead of the all the client desktop.
For both cases, we can use Trace.Write and Trace.Warn to display trace information. Warn method will display message in red. These 2 methods have the following format:
Trace.Write(”Category”,”Msg”);
Trace result is shown at the bottom of the page, which consists of the following information:
1. Request Details
2. Trace Information
3. Control Tree
4. Cookies Collection
5. Headers Collection
6. Form Collection
7. Server Variables
The priority of enabling trace information is from page (high) to Application (low). If page-level trace is enabled, regarding of whether application-level trace is enabled, the trace information is shown.
Trace into a Component
It is possible to disable the trace on page level, but enable it on a desired component. With the following steps:
1. Use System.Web namespace
2. Turn on the trace on a component. HttpContext.Current.Trace.IsEnabled = true,
3. Write something, HttpContext.Current.Trace.Write(”Category”,”msg”);
When trace is enabled on a component, trace is shown on any page that access to the components, even if trace is disabled for that page.
If trace on a component is ON,trace on page is OFF.
–> Trace message on component is shown but on page is not shown
If trace on a component is OFF, trace on page is ON
–> Trace message on component is NOT shown.
If trace on a component is not set, trace on page is ON
–> Trace message on component and page is shown
trace listener and trace switches??