In a talk I gave last week in London, someone in the
audience asked whether it was possible to automatically output all of the local
variables when a TracePoint was hit.
This capability isn’t built-in to Visual Studio – but can be
enabled by writing a custom Macro in Visual Studio, and then wiring up a
TracePoint to call the Macro when it is hit. To enable this, open up the
Macros IDE within Visual Studio (Tools->Macros->Macros IDE menu
command). Then under the MyMacros node in the project explorer, select a
module or create a new one (for example: add one named “UsefulThings”).
Then paste the following VB macro code into the module and save it:
Sub DumpLocals()
Dim outputWindow As EnvDTE.OutputWindow
outputWindow =
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
Dim currentStackFrame As EnvDTE.StackFrame
currentStackFrame = DTE.Debugger.CurrentStackFrame
outputWindow.ActivePane.OutputString("*Dumping Local Variables*" + vbCrLf)
For Each exp As EnvDTE.Expression In currentStackFrame.Locals
outputWindow.ActivePane.OutputString(exp.Name + " = " +
exp.Value.ToString() + vbCrLf)
Next
End Sub
The above macro code loops through the current stack frame
and dumps all local variables to the output window.