Debugging and Tracing in ASP.NET Ajax | WebReference

Debugging and Tracing in ASP.NET Ajax

By Joydip Kanjilal


Sometimes while you're developing an application you need to track potential errors that can creep in post-deployment. Debugging and tracing are two features that come in handy for tracking such errors. In the case of ASP.NET Ajax applications, debugging is a challenge because these applications contain a mix of server-side and client-side code. The client-side code can contain scripts, styles, HTML markups, etc.

However, you can overcome these obstacles and continue to debug and trace your ASP.NET Ajax applications much the same way you debug and trace an ASP.NET application. This article explains how you can use debugging and tracing functionalities in ASP.NET Ajax applications.

First, what exactly is Ajax? Ajax is an acronym for Asynchronous JavaScript and XML. It is a combination of multiple technologies that are used for building rich and responsive user interfaces. In fact, Ajax has quickly become the technology of choice for building lightning fast and responsive user interfaces. The sections that follow explore how you can use debugging and tracing functionalities in your ASP.NET Ajax applications.

How to Enable and Perform Debugging

Debugging is the process of setting breakpoints in code and stepping through the code to track potential errors. The Debug class in the Sys namespace provides support for tracing and debugging functionality in your ASP.NET Ajax applications. You can use any one or more of the following to enable debugging support in your ASP.NET Ajax applications:

  • Enable debugging support in the web browser or in the application's web.config file.
  • Attach the Visual Studio debugger to the web browser in use.
  • Use the Sys.Debug class.

To actually debug your ASP.NET Ajax application, you can follow one or more of the following approaches:

  • Enable debugging in the application's configuration file.
  • Use server-side tracing.
  • Use the Sys.Debug class.
  • Enable debugging in the web browser.
  • Attach the Visual Studio 2008 or Visual Studio 2010 debugger to the current Internet Explorer instance.
  • Use external tools to trap the HTTP traffic.

To enable debugging support in your Ajax-enabled ASP.NET application, you can write the following code in your application's web.config file:

Similarly, you can disable debugging support at deployment time to improve your application's performance.

There are a few points you should note here. You should set the release version of the application just before deploying it to release mode. Also, you should set the ScriptMode property of the ScriptManager controls used in your Ajax-enabled ASP.NET pages to release prior to deployment. Also, in order for you to use debugging in your ASP.NET Ajax applications, you should set the IsDebuggingEnabled attribute to true in the application's web.config file and set the ScriptMode attribute to Debug as shown here:

The ASP.NET Ajax documentation states: "The ASP.NET AJAX architecture provides a model for release and debug modes. Release mode provides error checking and exception handling optimized for performance, with minimized script size. Debug mode provides more robust debugging features, such as type and argument checking. If you create debug versions of your client scripts or script resources, ASP.NET runs the debug versions when the application is in debug mode. This enables you to throw exceptions in debug scripts while minimizing the size of release code."

You can start your application in debug mode by pressing the F5 key. You can also attach the Visual Studio Debugger to the Internet Explorer instance when the application is in execution. To do this, you need to select the Attach to Process option in the Debug menu of your Visual Studio IDE and then choose the Internet Explorer instance to which you would like your debugger attached.

To enable debugging support in Internet Explorer you can follow these steps:

  1. From the Tools menu of the browser, select Internet Options.
  2. Switch to the Advanced tab and clear the Disable Script Debugging checkbox.
  3. Next, select the Display Notification checkbox and turn off friendly messages.

The Sys.Debug class contains the following methods to display debug and trace information:

  • Sys.Debug.trace(message)
  • Sys.Debug.clearTrace()
  • Sys.Debug.assert(condition, message, displayCaller)
  • Sys.Debug.traceDump(object,name)