频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

怎样在 Visual C# .NET 中跟踪和调试(2)

作者: jsyzzcx 出处:CSDNBlog 2008-05-14 14:20    砖    好    评论   进入论坛
阅读提示:怎样在 Visual C# .NET 中跟踪和调试?当程序运行时,您可以使用 Debug 类的方法来生成消息,以帮助您监视程序执行顺序、检测故障或提供性能度量信息。默认情况下,Debug 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的“输出”窗口中。

确认它可以使用

1. 确保 Debug 是当前的解决方案配置。
2. 如果“解决方案资源管理器”窗口不可见,请按 CTRL+ALT+L 组合键以显示此窗口。
3. 右键单击“conInfo”,然后单击“属性”。
4. 在 conInfo 属性页左窗格中,在“配置”文件夹下,请确保箭头指向“调试”。
5. 在“配置”文件夹上面的“配置”下拉列表框中,单击“活动(调试)”或“调试”,然后单击“确定”。
6. 按 CTRL+ALT+O 以显示“输出”窗口。
7. 按 F5 键以运行该代码。在出现“断言失败”对话框时,单击“忽略”。
8. 在“控制台”窗口中,按 ENTER 键。此时程序即已完成,“输出”窗口应显示以下输出:

Debug Information-Product Starting
The product name is Widget
The available units on hand are100
The per unit cost is 1.03
System.Xml.XmlDocument
Field: The product name is Widget
Field: The units on hand are100
Field: The per unit cost is1.03
Calc: Total Cost is 103
This message WILL appear
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
Message will appear since dUnitcost < 1 is false
---- Assert Long Message ----
at Class1.Main(String[] args) \class1.cs(34)
The product name is Widget
The available units on hand are100
The per unit cost is 1.03
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

9. “控制台”窗口和 Output.txt 文件应显示以下输出:

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 文件与 conInfo 可执行文件 (conInfo.exe) 位于同一目录中。通常情况下,该目录是存储项目源的 \bin 文件夹,默认情况下为 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin。
完整代码列表

using System; 
using System.Diagnostics; 
class Class1
{
[STAThread]
static void Main(string[] args)
{
string sProdName = "Widget"; 
int iUnitQty = 100; 
double dUnitCost = 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.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 > 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 since dUnitcost < 1 is false"); 
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); 
Debug.Listeners.Add(tr1); 
TextWriterTraceListener tr2 = 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(); 
}
}

注意:要使此代码示例发挥作用,必须通过将 using System.Diagnostics; 粘贴为第一行代码来添加 System.Diagonstics 名称空间。

【相关文章】

【责任编辑:莺锦 TEL:(010)68476606】


共2页: 上一页 [1] 2
【内容导航】
专题
初探敏捷开发
WCF开发基础
体验Visual Studio 2008的魅力
Visual Studio 2005开发基础
测试开发人员参考手册
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看77373次)
·UML类图详解 (查看68019次)
·C++是垃圾语言?! (查看37703次)
·C#技术开发指南 (查看37075次)
·UML统一建模语言 (查看37027次)
·Java编程开发手册 (1196个砖)
·Java基础教程 (430个砖)
·C#技术开发指南 (311个砖)
·.NET开发手册 (246个砖)
·PB开发教程 (223个砖)
·Java编程开发手册 (657个好)
·Java基础教程 (578个好)
·.NET开发手册 (277个好)
·PB开发教程 (212个好)
·Delphi开发技术手册 (197个好)
订阅技术快讯
电子杂志下载
名称:SQL Server数据库管理精品黄皮书
简介:书中文章经过精挑细选,便于用户能根据自己的实际工作和学习,快速在本书寻找到相关资料。内容涵盖了SQL Server的安装与升级、语句查询、数据备份和恢复、自动化任务、数据同步、数据字典、安全和预防、性能和优化、集群等各方面应用信息,以及DBA管理人员在数据库管理工作中
名称:2007路由技术大全
简介:《2007路由技术大全》由51CTO.com网站特别策划制作,该书包括路由器技术、路由器产品、路由器配置、安全设置、路由器故障处理、路由器密码恢复,以及广大网友在实践使用中的心得经验和技巧文章,内容注重实用性,适用于初学者入门,也适合多年从业者提高,是一本实践和理论完
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。
汶川大地震 IT技术人为同胞生命祈福
汶川大地震 IT技..
访问控制列表(ACL)介绍
访问控制列表(ACL..
视频访谈:网管员如何踏上高薪之路
视频访谈:网管员..
· 视频访谈:网管员如何踏..
· 首届中国IT工程师生态..
· 华为路由器配置
· 全面解析35岁技术人的..
· 企业数据恢复指南
· 龙芯要做中国的“奔腾”
· 2008年上半年全国软考..
· 交换机故障解决指南
· Vista SP1对决XP SP3
· LAMP技术精解
· 微软出价446亿美元收购..
· 网络故障排除宝典
· AIX操作系统管理应用(..
· 华为员工自杀频频拷问..
· 三层交换技术专题
· ARP攻击防范与解决方案
ARP攻击防范与解决方案
ARP攻击防范与解..
iSCSI应用与发展
iSCSI应用与发展
SQL Server 2008/2005全解
SQL Server 2008/..
· SQL Server 2008/2005..
· SOA 面向服务架构
· SQL Server 2008/2005..
· iSCSI应用与发展
· RAID——磁盘阵列基础
· Apache技术专题
· 三层交换技术专题
· SQL Server入门到精通
· Apache技术专题
· 国际文档格式标准开战
· 路由器设置与口令恢复
· 打造安全服务器
· PHP开发应用手册
· SOA 面向服务架构
· 企业数据恢复指南
· ADSL应用面面俱到
ARP攻击防范与解决方案
ARP攻击防范与解..
SQL Server 2008/2005全解
SQL Server 2008/..
iSCSI应用与发展
iSCSI应用与发展
· iSCSI应用与发展
· SQL Server入门到精通
· SQL Server 2008/2005..
· SOA 面向服务架构
· Apache技术专题
· iSCSI应用与发展
· 三层交换技术专题
· Apache技术专题
· 企业数据恢复指南
· RAID——磁盘阵列基础
· 路由器设置与口令恢复
· SOA 面向服务架构
· ADSL应用面面俱到
· ADSL应用面面俱到
· 反垃圾邮件技术应用
· 访问控制列表(ACL)介绍