
最正规的调试,那就是配合系统的断点调试功能,把要调试的页面设置为起始页,在写代码的最左边左机一下设置一下断点,步骤如下:
按下F5,程序进入了调试的页面,左边有个黄色的箭头,表示程序调试到这里。这里有一点需要注意,就是黄色这句还没有执行。调试的命令这里备注一下F5表示的继续的执行到下一个断点,不管中间是什么代码,程序顺序的执行到下一个断点。如果没有断点,那就直接跳转到运行状态了。
2.按下F10,表示单句的执行调试命令,但是如果遇到调用函数之类就直接跳过函数调试,如果是函数有问题那就按F11如果函数没问题,那就按f10跳过函数的调试。f10和f11唯一的区别就是是否调试函数。
本文介绍如何使用 Debug
当程序运行时 您可以使用 Debug 类的方法来生成消息 以帮助您监视程序执行顺序 检测故障或提供性能度量信息 默认情况下 Debug 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的 输出 窗口中
该代码示例使用 WriteLine 方法生成后面带有行结束符的消息 当您使用此方法生成消息时 每条消息在 输出 窗口中均显示为单独的一行
使用 Debug 类创建一个示例 启动 Visual Studio NET
新建一个名为 conInfo 的新 Visual C# NET 控制台应用程序项目 将创建 Class
在 Class 的顶部添加以下名称空间
using System Diagnostics
要初始化变量以使其包含产品的相关信息 请将下面的声明语句添加到 Main 方法
string sProdName = Widget int iUnitQty = double dUnitCost =
(就在上面代码后面)直接输入将类生成的消息指定为 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 ToString())Debug WriteLine( The per unit cost is + dUnitCost ToString())
您还可以使用 WriteLine 方法显示现有对象的名称空间和类名称 例如 下面的代码在 输出 窗口中显示 System Xml XmlDocument 命名空间
System Xml XmlDocument oxml = new System Xml XmlDocument()Debug WriteLine(oxml)
要整理输出 可以包括一个类别作为 WriteLine 方法的第二个可选的输入参数 如果您指定一个类别 则 输出 窗口消息的格式为 类别:消息 例如 以下代码的第一行在 输出 窗口中显示 Field:The product name is Widget
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 ToString() Field )Debug WriteLine( Total Cost is + (iUnitQty * dUnitCost) Calc )
仅在使用 Debug 类的 WriteLineIf 方法将指定条件计算为 true 时 输出 窗口才可以显示消息 将要计算的条件是 WriteLineIf 方法的第一个输入参数 WriteLineIf 的第二个参数是仅在第一个参数的条件计算为真时才显示的消息
Debug WriteLineIf(iUnitQty >This message WILL appear )Debug WriteLineIf(iUnitQty <This message will NOT appear )
使用 Debug 类的 Assert 方法 使 输出 窗口仅在指定条件计算为 false 时才显示消息
Debug Assert(dUnitCost >Message will NOT appear )Debug Assert(dUnitCost <Message will appear since dUnitcost <is false )
为 控制台 窗口 (tr ) 和名为 Output txt (tr ) 的文本文件创建 TextWriterTraceListener 对象 然后将每个对象添加到 Debug Listeners 集合中
TextWriterTraceListener tr = new TextWriterTraceListener(System Console Out)Debug Listeners Add(tr )TextWriterTraceListener tr = new TextWriterTraceListener(System IO File CreateText( Output txt ))Debug Listeners Add(tr )
为了清晰易读 请使用 Unindent 方法去除 Debug 类为后续消息生成的缩进 当您将 Indent 和 Unindent 两种方法一起使用时 读取器可以将输出分成组
Debug Unindent()Debug WriteLine( Debug Information Product Ending )
为了确保每个 Listener 对象收到它的所有输出 请为 Debug 类缓冲区调用 Flush 方法
Debug Flush()
使用 Trace 类
您还可以使用 Trace 类生成监视应用程序执行的消息 Trace 和 Debug 类共享大多数相同的方法来生成输出 这些方法包括
◆WriteLine
◆WriteLineIf
◆Indent
◆Unindent
◆Assert
◆Flush
您可以在同一应用程序中分别或同时使用 Trace 和 Debug 类 在一个 调试解决方案配置 项目中 Trace 和 Debug 两种输出均为活动状态 该项目从这两个类为 Listener 对象生成输出 但是 发布解决方案配置 项目仅从 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 >This message WILL appear )Trace Assert(dUnitCost >Message will NOT appear )Trace Unindent()Trace WriteLine( Trace Information Product Ending )Trace Flush()Console ReadLine()
确认它可以使用
确保 Debug 是当前的解决方案配置 如果 解决方案资源管理器 窗口不可见 请按 CTRL+ALT+L 组合键以显示此窗口 右键单击 conInfo 然后单击 属性 在 conInfo 属性页左窗格中 在 配置 文件夹下 请确保箭头指向 调试 在 配置 文件夹上面的 配置 下拉列表框中 单击 活动(调试) 或 调试 然后单击 确定 按 CTRL+ALT+O 以显示 输出 窗口 按 F 键以运行该代码 在出现 断言失败 对话框时 单击 忽略 在 控制台 窗口中 按 ENTER 键 此时程序即已完成 输出 窗口应显示以下输出
Debug Information Product Starting The product name is Widget The available units on hand are The per unit cost is System Xml XmlDocument Field: The product name is Widget Field: The units on hand are Field: The per unit cost is Calc: Total Cost is This message WILL appear DEBUG ASSERTION FAILED Assert Short Message Message will appear since dUnitcost <is false Assert Long Message at Class Main(String[] args) \class cs( ) The product name is Widget The available units on hand are The per unit cost is Debug Information Product Ending Trace Information Product Starting The product name is Widget Field: The product name isWidget This message WILL appear Trace Information Product Ending
控制台 窗口和 Output txt 文件应显示以下输出
The product name is Widget The available units on hand are The per unit cost is 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 文件与 conInfo 可执行文件 (conInfo exe) 位于同一目录中 通常情况下 该目录是存储项目源的 \bin 文件夹 默认情况下为 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin
完整代码列表
using Systemusing System Diagnosticsclass Class { [STAThread] static void Main(string[] args) { string sProdName = Widget int iUnitQty = double dUnitCost = Debug WriteLine( Debug Information Product Starting )Debug Indent()Debug WriteLine( The product name is +sProdName)Debug WriteLine( The available units on hand are +iUnitQty ToString())Debug WriteLine( The per unit cost is + dUnitCost ToString())System Xml XmlDocument oxml = 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 ToString() Field )Debug WriteLine( Total Cost is +(iUnitQty * dUnitCost) Calc )Debug WriteLineIf(iUnitQty >This message WILL appear )Debug WriteLineIf(iUnitQty <This message will NOT appear )Debug Assert(dUnitCost >Message will NOT appear )Debug Assert(dUnitCost <Message will appear since dUnitcost <is false )TextWriterTraceListener tr = new TextWriterTraceListener(System Console Out)Debug Listeners Add(tr )TextWriterTraceListener tr = new TextWriterTraceListener(System IO File CreateText( Output txt ))Debug Listeners Add(tr )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 >This message WILL appear )Trace Assert(dUnitCost >Message will NOT appear )Trace Unindent()Trace WriteLine( Trace Information Product Ending )Trace Flush()Console ReadLine()} }
lishixinzhi/Article/program/net/201311/137101、拷贝project1的dll及相关的xap及页面文件到对应的project2工程中,不必加入到工程中。
2、启动Project2工程【Ctrl+F5】,不调试,启动起来之后设置project1【ctrl+alt+p】附加到进程窗口
找到相关的进程【如果是web程序找到对应端口号的那个,是WebDev开头的那个】,Attach(附加)上就可以了。
3、在需要调试的地方设置断点,运行就可以了。
4、如果是Silverlight程序Select一下选择调试的代码是Silverlight,这个很重要,如果自动选择的话有可能无法调试到Silverlight程序中,我碰到过一次这样的问题。
5、如果还是进入不了断点,在工具->选项->调试,取消要求与源代码一致的选项
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)