Notes
Slide Show
Outline
1
Debugging .NET Applications With
Visual Studio® .NET
2
Overview
  • What’s new
  • Debugging
    • .NET applications
    • asp.net
    • SQL Server
    • Cross language
  • Instrumentation
  • Hidden goodies
3
What’s New
  • Unified user interface
  • No memory leaks
    • Leaks and corruptions are a thing of the past!
  • Real remote debugging
    • Single step from client to server, to server, to server!
  • Attach and detach from running servers
    • Debug production servers without killing them!
  • Cross-language debugging
4
VS.NET Debugger Basics
  • The UI has improved dramatically
    • Breakpoints dialog is dockable
    • Multiple memory windows (finally!)
    • More breakpoint modifiers
  • Changes are for both managed and unmanaged code
5
 
6
Setting Up for Debugging
  • For ASP.NET with script on the pages
    • For a single page, use Page directive

  • Set application-wide debugging in Web.Config
7
Setting Up for Debugging
  • For WINFORMS and ASP.NET compiled components
    • Build with debug symbols
8
Debugging .NET Executables
  • Same as today
    • Hit F5
    • Manual attach
  • Customize F5
    • Remote launch
    • Launch URL
    • Launch executable
    • Launch debuggers
9
Debugging .NET dll’s
  • Must specify host process
    • Specify host in project properties, or
    • Manually attach
  • “Additional DLL’s” improvement!
10
JIT Debugging
  • “Just In Time” debugging
    • Exception occurs - automatic attach
    • Default is off for Windows Forms
    • Machine.Config or Application.Exe.Config




  • Debugger choice
11
Debugging Applications Built Outside The IDE
  • Two ways
    • Compile with /debug
      • Generates debugging info (pdb file)
      • Launch then attach
    • Open your exe as a project
      • Hit F5
12
 
13
Debugging asp.net Applications
  • F5 – just works
    • Automatic attach to Web server process
  • To debug running applications
    • Manually attach to asp.net
    • Typically “aspnet_wp.exe”
  • Detach from process!
14
Debugging Web Services
  • F5 – just works
    • Launches Web Service “help” page
    • Wait for Web Service to be called
  • Step into Web Services
15
 
16
Remote Debugging asp.net
17
SQL Server Debugging
  • Two ways
    • Use server explorer
      • Debug stored procedures + functions
    • Call procedure from application
      • Set breakpoint
      • Enable SQL debugging
  • Debug into triggers
18
 
19
Cross Language Debugging
  • Debug across .NET languages
  • Debug from .NET to C++
    • Platform Invocation Services (P/Invoke)
    • COM Interoperability
    • IJW for Managed Extensions for C++
  • Debug into TSQL
20
 
21
Instrumentation
22
WinForm Tracing & Asserts
  • System.Diagnostics has two classes for tracing and assertions
    • Trace
    • Debug
  • Both classes have identical members
  • Only active when conditionally compiled with TRACE and DEBUG, respectively
    • Always build with TRACE defined
23
Tracing Methods
  • Four methods
    • Write, WriteIf, WriteLine, WriteLineIf
  • *If methods only do output if first parameter evaluates to true
    • Allows conditional tracing
  • Watch for performance hit with *If
24
Tracing Methods
25
Conditional Tracing
  • In team development, tracing effectiveness can go to zero quickly
    • Too much output
  • .NET provides the TraceSwitch class
    • Allows easy per assembly/module/class tracing
    • From the System.Diagnostic namespace
26
Tracing Levels
  • TraceSwitch also allows you to easily set the tracing level to
    • 0 - None
    • 1 - Errors only
    • 2 - Warnings (Errors as well)
    • 3 - Info (Warnings and Errors also)
    • 4 - Verbose (All traces turned on)
27
Using TraceSwitches
  • Using a TraceSwitch is trivial
28
Setting Trace Switches
  • Put the switches in XML configuration files associated with the EXE
    • This is the only way to set them
  • File name is <program>.EXE.CONFIG
    • “name” is the switch name”
    • “value” is the numerical value
29
Setting Trace Switches
30
Trace Output
  • The tracing output is controlled by TraceListeners
  • The DefaultTraceListener trace output
    • Any attached .NET debugger
    • To Win32 OutputDebugString calls
31
Tracing Output
  • Other TraceListeners
    • TextWriterTraceListener
      • Output to a text file
    • EventLogTraceListener
      • Output to the event log
  • Adding a TraceListener in code is trivial
32
Tracing Thoughts
  • Since Trace and Debug both have the same methods, which do you use for tracing?
    • Use Trace strictly for tracing
  • Each assembly should have it’s own trace switch
33
Assertions
  • Both Trace and Debug objects have an Assert method
  • With the DefaultTraceListener, assertion output is to a message box with full call stack with source and line information
34
Assertions
  • .NET assertions require slightly more typing to get the assertion information in them
  • You have to type the condition twice if you want it to appear in the message box
35
Assertions
  • You can type multiple messages if you really want
36
Assertions
  • Which Assert method do you use, Debug’s or Trace’s?
  • I opt for using Debug’s as assertions are debug compilation features
  • Remember:
    • Debug.Assert
    • Trace.Write
  • Whatever you choose, pick one way and stick with it!
37
Assertion Output
  • To turn off the default message boxes in <prog>.EXE.CONFIG using the current TraceListeners
38
Assertion Output
  • You can also change the output by setting the Debug object TraceListeners in code
39
ASP.NET Tracing & Assertions
  • ASP.NET tracing is completely different
  • The System.Web.UI.Page object provides a TraceContext object in the Trace property
  • Tracing is disabled by default
40
Page Level Tracing
  • At the top of the page add the @Page directive


  • Trace.Write and Trace.Warn do the writing
    • Output appears at the bottom of the page
    • Write appears in black
    • Warn appears in red
41
Application Level Tracing
  • The <trace> section of Web.Config is where you set the parameters
42
The Trace Log
  • When pageOutput is false, output goes to the trace log
  • Append “trace.axd” onto the applications root directory to view
43
 
44
Hidden Goodies
  • Debugger automation model
  • Expand your types automatically
    • Mcee_cs.dat for C#
    • Mcee_mc.dat for MC++
    • UserType.dat
  • Reload Symbols
  • Exceptions dialog
45
Questions