spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp / debugging To page 1To page 2current pageTo page 4
[previous] [next]

Debugging and Tracing in ASP.NET

Information Technology Auditor (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

System.Diagnostics.Debug

This class provides methods for writing debugging output and validating assertions. It has an Assert and Fail method with multiple signatures to suit different situations. The methods of this class are defined with the Conditional attribute discussed earlier. So the compiler variable DEBUG must be defined for these methods to be called. In Visual Studio .NET DEBUG is defined by default during debug builds. Thus, the use of the methods of this class does not affect performance or size of release builds.

When debugging output is written using the four different Write methods in this class they go to all the listeners that are registered with this class. All listeners that can trace debug output inherit from TraceListener, which is an abstract class. There are three concrete listeners that inherit TraceListener. The first listener, which is always registered by default, is the DefaultTraceListener. This prints the output to the debugger log during debugging. The second concrete listener is EventLogTraceListener. This directs its output to an event log. The third concrete listener is the TextWriterTraceListener. This prints its output to a text file.

When controlling the debugging trace with a BooleanSwitch we have two options. One is to use an if...else statement as shown below.

if(traceflag.Enabled) 
{
	Debug.Write("Some debugging ouput");
}

The other is to use the provided WriteIf methods of the Debug class.

Debug.WriteIf(debug.Enabled, "Some debugging ouput");

Given these two options it is always better to use the if...else structure for performance reasons. When the traceflag is disabled the call to Write is never performed. However, in the second construct the boolean check and the call is always performed. Thus, for all practical purposes the Write methods should be used exclusively.

Setting the appropriate properties of the Debug class can control the format of the debugging output. While all properties and listeners can be added in code it is best that they are done in the config file. This allows for changes without recompilation. The example config file shown above can be modified to add a text file log and fix some formatting options as shown below.

<configuration>
 	
	...

    <system.diagnostics>
       	<switches>
          <add name="debugSwitch" value="1" />
        </switches>
	<debug autoflush="true" indentsize="4">
           <listeners>
              <add name="textListener" 
                   type="System.Diagnostics.TextWriterTraceListener,System" 
                   initializeData="c:\Debug.log" />
           </listeners>
	</debug>
    </system.diagnostics>
</configuration>

System.Diagnostics.StackTrace

Many times it is helpful to print out the call stack. This class provides the relevant functionality. Unlike Java where a stack trace can only be obtained for an exception by calling the printStackTrace method, the .NET framework provides the ability to construct a stack trace anywhere in the code by constructing a new instance of the StackTrace object. The call stack pertaining to an exception can also be generated using the appropriate constructor. Once the StackTrace object has been created the content can be printed out for observation.

A StackTrace object is essentially a collection of System.Diagnostics.StackFrame objects. Each StackFrame object contains information about one call. The StackTrace object exposes this collection using the FrameCount property and the GetFrame method.

The following code prints out the call stack using the StackTrace and StackFrame class.

StackTrace st = 
   new System.Diagnostics.StackTrace(true);

for(int i = 0; i < st.FrameCount; i ++)
{
	StackFrame sf = st.GetFrame(i);
	Debug.WriteLine(" File: " +  sf.GetFileName()  +
			" Line: "  + sf.GetFileLineNumber() + 
			" Method: " + sf.GetMethod());
}

home / programming / asp / debugging To page 1To page 2current pageTo page 4
[previous] [next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Review: Lenovo ThinkPad SL300 · OCZ PC3-10666 Gold 2x1GB Review · Apple Recommends Antivirus for Macs

Created: December 4, 2002
Revised: December 4, 2002

URL: http://webreference.com/programming/asp/debugging/3.html