你也可以选择往应用程序中添加一个自定义追踪信息的监听器。这时追踪点的输出信息将通过它输出,而不是Visual Studio的“输出“窗口。 跟踪点—运行自定义的宏
上周,我在伦敦演讲时,有听众问道:当命中跟踪点时,能否自动输出所有的局部变量?
Visual Studio中并没有这样的内置功能,但我们可以写一个自定义宏来实现,然后在命中跟踪点时调用该宏。这个的实现需要先打开Visual Studio的宏编辑器(工具->宏->宏IDE菜单命令),然后在项目资源管理器的MyMacros节点下选择一个模块或创建新模块(如:加个名为“UsefulThings”的模块),再把下面的VB宏代码贴到模块中并保存。
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
上述宏代码将循环当前的堆栈,把所有的局部变量输出到“输出”窗口。 使用自定义的“DumpLocals”宏
然后,我们可以在如下的一个简单程序中使用刚定制的“DumpLocals”宏了: