本文介绍如何使用 Debug Visual Basic .NET 中的类和 Trace 类。
原始产品版本: Visual Basic .NET
原始 KB 数: 313417
总结
本文演示如何使用 Debug 和 Trace 类。 Microsoft .NET Framework 中提供了这些类。 可以使用这些类提供有关应用程序开发期间或部署到生产环境后应用程序性能的信息。 这些类只是 .NET Framework 中提供的检测功能的一部分。
要求
以下列表概述了所需的推荐硬件、软件、网络基础结构和服务包:
- Windows
- Visual Basic .NET
本文还假定你熟悉程序调试。
技术说明
使用 Debug 类部分创建示例中的步骤演示如何创建一个控制台应用程序,该应用程序使用Debug该类提供有关程序执行的信息。
程序运行时,可以使用类的方法 Debug 生成有助于监视、检测故障或提供性能度量信息的消息。 默认情况下,类 Debug 生成的消息将显示在 Microsoft Visual Studio 集成开发环境(IDE)的“输出”窗口中。
示例代码使用 WriteLine 该方法生成后跟行终止符的消息。 使用此方法生成消息时,每个消息将显示在“输出”窗口中的单独行上。
如果使用 Assert 类的方法 Debug ,则仅当指定条件的计算结果为 false 时,“输出”窗口才会显示消息。 该消息也会显示在用户的模式对话框中。 该对话框包括消息、项目名称和 Debug.Assert 语句编号。 该对话框还包括三个命令按钮:
- 中止:应用程序停止运行。
- 重试:应用程序进入调试模式。
- 忽略:应用程序继续进行。 用户必须先单击其中一个按钮,然后应用程序才能继续。
还可以将类的 Debug 输出定向到“输出”窗口以外的目标。 该 Debug 类具有一个名为 Listeners 包含侦听器对象的集合。 每个侦听器对象监视 Debug 输出并将输出定向到指定的目标。 集合中的每个 Listeners 侦听器都接收类生成的任何输出 Debug 。 使用 TextWriterTraceListener 类定义侦听器对象。 可以通过类的构造函数指定类的目标 TextWriterTraceListener 。 一些可能的输出目标包括:
- 使用
System.Console.Out属性的控制台窗口。 - 使用语句的
System.IO.File.CreateText("FileName.txt"))文本文件。
创建 TextWriterTraceListener 对象后,必须将对象添加到 Debug.Listeners 集合以接收 Debug 输出。
使用 Debug 类创建示例
使用 Visual Basic .NET 创建新的名为 conInfo 的控制台应用程序项目。 默认情况下,命名
Module1的公共模块将添加到项目中。若要初始化变量以包含有关产品的信息,请添加以下
Dim语句:Dim sProdName As String = "Widget" Dim iUnitQty As Integer = 100 Dim dUnitCost As Decimal = 1.03指定类生成为方法的第一个输入参数
WriteLine的消息。 按 Ctrl+Alt+O 组合键以确保“输出”窗口可见。Debug.WriteLine("Debug Information-Product Starting ")若要提高可读性,请使用该方法
Indent在“输出”窗口中缩进后续消息:Debug.Indent()若要显示所选变量的内容,请使用
WriteLine如下所示的方法:Debug.WriteLine("The product name is " & sProdName) Debug.WriteLine("The available units on hand are " & iUnitQty) Debug.WriteLine("The per unit cost is " & dUnitCost)还可以使用
WriteLine该方法显示现有对象的命名空间和类名称。 例如,以下代码在“输出”窗口中显示System.Xml.XmlDocument命名空间:Dim oxml As New System.Xml.XmlDocument() Debug.WriteLine(oxml)若要组织输出,可以将类别作为方法的
WriteLine可选第二个输入参数包含在内。 如果指定类别,“输出”窗口消息的格式为“category: message”。例如,以下代码的第一行在“输出”窗口中显示“字段:产品名称为小组件” :Debug.WriteLine("The product name is " & sProdName, "Field") Debug.WriteLine("The units on hand are " & iUnitQty, "Field") Debug.WriteLine("The per unit cost is " & dUnitCost, "Field") Debug.WriteLine("Total Cost is" & iUnitQty * dUnitCost, "Calc")仅当指定的条件使用
WriteLineIf类的方法Debug计算结果为 true 时,“输出”窗口才能显示消息。 要计算的条件是该方法的第一个输入参数WriteLineIf。 第二个参数是仅在第一个参数WriteLineIf中的条件计算结果为 true 时显示的消息。Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear") Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear")使用类的
DebugAssert 方法,以便仅当指定条件的计算结果为 false 时,“输出”窗口才会显示消息:Debug.Assert(dUnitCost > 1, "Message will NOT appear") Debug.Assert(dUnitCost < 1, "Message will appear")为
TextWriterTraceListener控制台窗口(tr1)和名为Output.txt(tr2)的文本文件创建对象,然后将每个对象添加到DebugListeners集合中:Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New _ TextWriterTraceListener(System.IO.File.CreateText("Output.txt")) Debug.Listeners.Add(tr2)若要提高可读性,请使用
Unindent该方法删除类生成的后续消息Debug的缩进。 一Indent起使用和Unindent方法时,读取器可以将输出区分开来作为组。Debug.Unindent() Debug.WriteLine("Debug Information-Product Ending")若要确保每个侦听器对象接收其所有输出,请调用
FlushDebug类缓冲区的方法:Debug.Flush()
使用 Trace 类
还可以使用该 Trace 类生成监视应用程序执行的消息。 和TraceDebug类共享大多数相同的方法来生成输出,包括:
WriteLineWriteLineIfIndentUnindentAssertFlush
可以在 Trace 同一应用程序中单独使用和 Debug 类。 在调试解决方案配置项目中,输出都TraceDebug处于活动状态。 项目从这两个类生成所有侦听器对象的输出。 但是,发布解决方案配置项目仅生成类 Trace 的输出。 发布解决方案配置项目忽略任何 Debug 类方法调用。
Trace.WriteLine("Trace Information-Product Starting ")
Trace.Indent()
Trace.WriteLine("The product name is " & sProdName)
Trace.WriteLine("The product name is " & sProdName, "Field")
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Trace.Assert(dUnitCost > 1, "Message will NOT appear")
Trace.Unindent()
Trace.WriteLine("Trace Information-Product Ending")
Trace.Flush()
Console.ReadLine()
验证它是否正常工作
确保 调试 是当前解决方案配置。
如果解决方案资源管理器窗口不可见,请按 Ctrl+Alt+L 键组合以显示此窗口。
右键单击 conInfo,然后单击“ 属性”。
在 conInfo 属性页的左窗格中,在“配置”文件夹下,确保箭头指向“调试”。
在 “配置”文件夹上方的“配置 ”下拉列表框中,单击“活动” (调试) 或 “调试”,然后单击“ 确定”。
按 Ctrl+Alt+O 显示“输出”窗口。
按 F5 键运行代码。 出现断言失败对话框时,单击“ 忽略”。
在控制台窗口中,按 Enter。 程序应完成,“输出”窗口应显示以下输出:
Debug Information-Product Starting The product name is Widget The available units on hand are 100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are 100 Field: The per unit cost is 1.03 Calc: Total cost is 103 This message WILL appear ---- DEBUG ASSERTION FAILED ---- ---- Assert Short Message ---- Message will appear ---- Assert Long Message ---- at Module1.Main() C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\conInfo\Module1.vb(29) The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending控制台窗口和Output.txt文件应显示以下输出:
(The Output.txt file is located in the same directory as the conInfo executable, conInfo.exe. Normally this is the \bin folder of where the project source has been stored. By default that would be C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin) The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending
完整代码清单
Module Module1
Sub Main()
Dim sProdName As String = "Widget"
Dim iUnitQty As Integer = 100
Dim dUnitCost As Decimal = 1.03
Debug.WriteLine("Debug Information-Product Starting ")
Debug.Indent()
Debug.WriteLine("The product name is " & sProdName)
Debug.WriteLine("The available units on hand are " & iUnitQty)
Debug.WriteLine("The per unit cost is " & dUnitCost)
Dim oxml As New System.Xml.XmlDocument()
Debug.WriteLine(oxml)
Debug.WriteLine("The product name is " & sProdName, "Field")
Debug.WriteLine("The units on hand are " & iUnitQty, "Field")
Debug.WriteLine("The per unit cost is " & dUnitCost, "Field")
Debug.WriteLine("Total cost is " & iUnitQty * dUnitCost, "Calc")
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear")
Debug.Assert(dUnitCost > 1, "Message will NOT appear")
Debug.Assert(dUnitCost < 1, "Message will appear")
Dim tr1 As New TextWriter`Trace`Listener(System.Console.Out)
Debug.Listeners.Add(tr1)
Dim tr2 As New _
TextWriterTraceListener(System.IO.File.CreateText("Output.txt"))
Debug.Listeners.Add(tr2)
Debug.WriteLine("The product name is " & sProdName)
Debug.WriteLine("The available units on hand are " & iUnitQty)
Debug.WriteLine("The per unit cost is " & dUnitCost)
Debug.Unindent()
Debug.WriteLine("Debug Information-Product Ending")
Debug.Flush()
Trace.WriteLine("`Trace` Information-Product Starting ")
Trace.Indent()
Trace.WriteLine("The product name is " & sProdName)
Trace.WriteLine("The product name is " & sProdName, "Field")
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear")
Trace.Assert(dUnitCost > 1, "Message will NOT appear")
Trace.Unindent()
Trace.WriteLine("Trace Information-Product Ending")
Trace.Flush()
Console.ReadLine()
End Sub
End Module
故障排除
如果解决方案配置类型为 Release,则
Debug忽略类输出。为特定目标创建
TextWriterTraceListener类后,TextWriterTraceListener接收来自Trace该类和类的Debug输出。 无论使用Add类的方法DebugTrace还是将类添加到TextWriterTraceListenerListeners类,都会发生这种情况。如果在类
Debug中Trace为同一目标添加侦听器对象,则无论输出是DebugTrace还是生成输出,每行输出都会重复。Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New TextWriterTraceListener(System.Console.Out) Trace.Listeners.Add(tr2)