Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

VB.NET 2005 Tutorials

 
Home Tutorials VB.NET 2005
 

Web Application Tracing and Debugging

 

Web Application Tracing and Debugging

Page 1 of 3

Tracing and Debugging a Web Application

In this tutorial you will learn how to trace and debug a web application.
  • Tracing - Using the TraceContext class, Using Trace and Debug, Trace listeners, Trace Switches and Conditional Compilation.
  • Debugging - you will learn setting breakpoints and conditional breakpoints, how to debug a running process and also about debugging a remote process.


Tracing

This class is used to append messages to specific trace categories. For example, if the developer is creating an instance of the System.Web.UI.WebControls.Calendar class for the application, he might include the trace message "Starting To Render" in a rendering procedure, and "Firing OnChange Event" in an event handler procedure. The System.Web.TraceContext class is used to obtain a set of trace records at the end of request execution for custom processing. To enable tracing, set the Trace attribute in the [] directive or the System.Web.TraceContext.IsEnabled property to true. When tracing is enabled, in addition to showing user-provided trace content, the System.Web.UI.Page class automatically includes performance data, control-tree structure information, and state management content for every request.


Using the TraceContext class

The TraceContext Class captures and presents execution details about a web request. This class cannot be inherited. It is available in the System.Web namespace. This object can be declared and instantiated in the following manner.


Dim ctxt As HttpContext
Dim txtxt As New TraceContext(ctxt)


This class can be used to append messages to specific trace categories. If an instance of a calender class is being created for the application, a trace message may be included such as-- “Starting to render” in the rendering procedure, and “Firing OnChange Event” in an event handler procedure. In this case the TraceContext class can be used to obtain a set of trace records at the end of request execution for custom processing.


Using Trace and Debug

It is not always possible to use the debugger. In such circumstances tracing becomes a useful alternate. If the pages are hosted on a remote server the debugger may not be available. Tracing also makes it easy for the user see information, such as the HTTP header, that is not explicitly available in the debugger.


The website used for consuming web services may be used again to study the impact of tracing. To the solution add a new item by right-clicking on the project in the solution explorer. In the templates dialog box choose Web Form. In the name box type TraceTestPage.aspx. Now proceed to add one TextBox, one Button and a label.


Now add code to the page. When the page is running without any problem, the user can enter a name and click the button, which displays the name in the label control. If the user closes the browser and then comes back to this page, the page will already have the user’s name, because it is stored in a cookie.


To add code right-click on the name of the page in the solution explorer and click view code. Inside the class definition, you can enter the following code:


Partial Class TraceTestForm_aspx
Inherits System.Web.UI.Page
Sub buttonDisplayName_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Label1.Text = Server.HtmlEncode(TextBox1.Text)
Response.Cookies("username").Value = Label1.Text
End Sub
End Class


The code performs the following tasks:


It reads the value of the TextBox control and displays the name in the Label control. As part of this logic, the code calls the System.Web.HttpServerUtility.HtmlEncode method, which turns potentially executable HTML characters such as < into their display equivalents. This is a security measure to prevent scripting exploits. The code creates a cookie named username that stores the value that the user has entered.


When the application is built and executed, the web page appears. Type a name in the browser and click the submit button. When the post back of the page takes place, the posting takes place. The name is not displayed in the label1.


To enable tracing to be true, do the following:


1. Switch to Default.aspx and switch to Design view.
2. In the drop-down list at the top of the Properties window, select DOCUMENT.
3. This displays properties for the page.
4. Set Trace to true.


The trace setting is actually made as part of the @ Page directive. This can be seen by switching to Source view and looking at the first line in the page. The @ Page will look like this:


Sub buttonDisplayName_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Trace.Warn("debugging", "Start buttonDisplayName Click handler")
Label1.Text = Server.HtmlEncode(TextBox1.Text)
Response.Cookies("username").Value = Label1.Text
Trace.Warn("debugging", "End buttonDisplayName Click handler")
End Sub


The page when executed does not produce the desirable out put. The out put that appears is displayed below with the trace information:



When the trace output is examined, it is clear that the output contains no red text. You can conclude from this that the button's Click handler is not being called. Now add the following code to the program instead of the code available for the button. The code will look like the following listing:


Click here to view sample code


The output generated looks like the graphic shown below:



The desired result is achieved. The most common reason that a handler is not called is that the control has not been properly bound to the event handler. That is true in this case — although the event-handling code has been added, the button's Click event was not bound to the handler. (Normally, Visual Web Developer binds the event to the control when the control is double-clicked in Design view.). Now the corrected the code creates the desired result. The red colored text indicates that that the button’s click handler is called.


Next Page: Web Application Tracing and Debugging - Page 2


Read Next: ActiveX Controls and Legacy Code



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe