Sys.Debug in Asp.Net Ajax Framework

Every developer needs to debug his applications. There are two ways:

  • Debugging - step-by-step tracking of an application process.
  • Tracing - monitoring of application processes without breaking and writing useful information in a special console.

Asp.Net developers wrote the great Ajax Framework. There is so much stuff for professionals and beginner developers.
Also they didn't forget about debugging tools. (may be was done firstOpen-mouthed)

class Sys.Debug contains 5 methods:

   1:  // output the text message into Debug Console
   2:  Sys.Debug.trace(text);
   3:  // output the information about specified object into Debug Console.
   4:  // second parameter (object name) - is optional
   5:  Sys.Debug.traceDump(object, name);
   6:  // clear Debug Console*
   7:  Sys.Debug.clearTrace();
* - it doesn't clear Debug Console of your default Debugger(more information below the post).
   1:  // output the message only if(condition == true)
   2:  // if the third parameter is specified and set to true then name of the caller function will be shown
   3:  Sys.Debug.assert(condition, message, displayCaller);
   4:  // output the message into Debug Console and break program into default debugger in the invocation point
   5:  Sys.Debug.fail(message);
 
So, what does "Debug Console" mean?
Let`s see the core of Asp.Net Ajax Framework (MicrosoftAjax.js).
There are 2 interesting methods:
 
   1:  // output the text message into Debug Console window of your current debugger.
   2:  this._appendConsole(text);
   3:  // output the text message into textarea in the webform (more information below the text)
   4:  this._appendTrace(text);
Debugger Console may looks like that (Output window in your favorite Visual Studio):

 

VS_debug_console 

Or like that (Console tab in Firebug plugin for Mozilla Firefox):

firebug_output

Or something else.

 

So, what does second method do?

   1:  this._appendTrace(text);
Follow it source:
   1:  var traceElement = document.getElementById('TraceConsole');
   2:  if (traceElement && (traceElement.tagName.toUpperCase() === 'TEXTAREA'))
   3:  {
   4:       traceElement.value += text + '\n';
   5:  }

It looks up an element with ID="TraceConsole" in a webform and verify for the "textarea" type. If well done, message will be written into this element.

Exactly  that Console will be cleaned by the method

   1:  Sys.Debug.clearTrace();

Sys.Debug works only if:

  1. debug="true" in Web.config
   1:  <compilation debug="true">

      2.    ScriptMode="Debug" or ScriptMode="Inherit" (it will be inherited from web.config ) in ScriptManager in the page.

   1:  <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">

In conclusion I want to say, that Sys.Debug saves my time and nerves. What about you?

8 Comments

Comments have been disabled for this content.