C#跟踪和调试程序-Debug类使用
摘要:
怎样在 Visual C# .NET 中跟踪和调试?当程序运行时,您可以使用 Debug 类的方法来生成消息,以帮助您监视程序执行顺序、检测故障或提供性能度量信息。默认情况下,Debug 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的“输出”窗口中。
如何使用 Debug
当程序运行时,您可以使用 Debug 类的方法来生成消息,以帮助您监视程序执行顺序、检测故障或提供性能度量信息。默认情况下,Debug 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的“输出”窗口中。
该代码示例使用 WriteLine 方法生成后面带有行结束符的消息。当您使用此方法生成消息时,每条消息在“输出”窗口中均显示为单独的一行。
使用 Debug 类创建一个示例
1. 启动 Visual Studio .NET。
2. 新建一个名为 conInfo 的新 Visual C# .NET 控制台应用程序项目。将创建 Class1。
3. 在 Class1 的顶部添加以下名称空间。
using System.Diagnostics;
4. 要初始化变量以使其包含产品的相关信息,请将下面的声明语句添加到 Main 方法:
string sProdName = "Widget";
int iUnitQty = ;
double dUnitCost = 1.03;
5. (就在上面代码后面)直接输入将类生成的消息指定为 WriteLine 方法的第一个输入参数。按 CTRL+ALT+O 组合键以确保“输出”窗口可见。
Debug.WriteLine("Debug Information-Product Starting ");
6. 为了清晰易读,请使用 Indent 方法在“输出”窗口中缩进后面的消息:
Debug.Indent();
7. 要显示所选变量的内容,请使用 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());
8. 您还可以使用 WriteLine 方法显示现有对象的名称空间和类名称。例如,下面的代码在“输出”窗口中显示 System.Xml.XmlDocument 命名空间:
System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
Debug.WriteLine(oxml);
9、要整理输出,可以包括一个类别作为 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");
10. 仅在使用 Debug 类的 WriteLineIf 方法将指定条件计算为 true 时,“输出”窗口才可以显示消息。将要计算的条件是 WriteLineIf 方法的第一个输入参数。WriteLineIf 的第二个参数是仅在第一个参数的条件计算为真时才显示的消息。
Debug.WriteLineIf(iUnitQty > , "This message WILL appear");
Debug.WriteLineIf(iUnitQty < , "This message will NOT appear");
11. 使用 Debug 类的 Assert 方法,使“输出”窗口仅在指定条件计算为 false 时才显示消息:
Debug.Assert(dUnitCost > , "Message will NOT appear");
Debug.Assert(dUnitCost < , "Message will appear since dUnitcost < 1 is false");
12. 为“控制台”窗口 (tr1) 和名为 Output.txt (tr2) 的文本文件创建 TextWriterTraceListener 对象,然后将每个对象添加到 Debug Listeners 集合中:
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("d:/Output.txt"));
//System.Diagnostics.TextWriterTraceListener tr2 = new System.Diagnostics.TextWriterTraceListener(@"d:/Output.txt");
//System.Diagnostics.TextWriterTraceListener tr2 = new System.Diagnostics.TextWriterTraceListener(@"Output.txt");
Debug.Listeners.Add(tr2);
说明:
tr1定义:将调试信息,即Debug输出通过控制台程序输出。
tr2定义,将调试信息,即Debug内容输出到Output.txt文件,通常需要tr2.Flush()才能输出到文本文件,或者调用Debug.Flush(),将全部TextWriterTraceListener对象的调试缓冲输出。
不指定路径盘符,则输出的调试文件保存在程序的运行目录下。
13. 为了清晰易读,请使用 Unindent 方法去除 Debug 类为后续消息生成的缩进。当您将 Indent 和 Unindent 两种方法一起使用时,读取器可以将输出分成组。
Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");
14. 为了确保每个 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();
System.Diagnostics.Trace.WriteLine("Trace Log");
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.AutoFlush = true;
System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener("app.log"));
C#跟踪和调试程序-Debug类使用的更多相关文章
- System.Diagnostics命名空间里的Debug类和Trace类的用途
在 .NET 类库中有一个 System.Diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类--Debug ...
- debug类和trace类的区别
在 .net 类库中有一个 system.diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类——debug ...
- ASP里面令人震撼地自定义Debug类(VBScript)
不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便 我想可能很多朋友都会用这样的方法“response.write ”,然后输出相关的语句来看看是否正确.前几天 ...
- Unity Debug类
静态变量 developerConsoleVisible 报告是否开发控制台是可见的.开发控制台不能出现使用: isDebugBuild 在构建设置对话框中有一个叫做"发展构建"复 ...
- 【转载】汇编调试程序Debug使用
https://blog.csdn.net/Notzuonotdied/article/details/70888205
- C#在代码中编写输出debug信息-类Debug的使用
文章:C# 的两种debug 方法 文章:C#跟踪和调试程序-Debug类使用 很全面的文章,可以仔细学习使用下. 文章:C#调试类 没有仔细看. 关键字:Debug类和Trace类有什么区别? 微软 ...
- Debug与Trace工具类的应用
在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...
- System.Diagnostics.Debug和System.Diagnostics.Trace 【转】
在 .net 类库中有一个 system.diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类——debug ...
- debug,trace,release项目配置区别
Debug模式是用来调试用的,它生成的执行文件中含有大量调试信息,所以很大: Release模式生成的执行文件消除了这些调试信息,可用来作为成品发布 Debug只在debug状态下会输出,Trace在 ...
随机推荐
- js如何把字符串转换成json数据的方法
js如何把字符串转换成json数据的方法 function strtojson(str){ var json = eval('(' + str + ')'); return json; } 方法二 f ...
- html部分---格式与布局;
一:position:fixed(相对于浏览器窗口来对元素进行定位) <style type="text/css"> .aa { position:fixed; lef ...
- Prim求解最小生成树
#include "ljjz.h" typedef struct edgedata /*用于保存最小生成树的边类型定义*/ { int beg,en; /*beg,en是边顶点序号 ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- timus 1210 Kind Spirits(最短路)(动态规划)
Kind Spirits Time limit: 1.0 secondMemory limit: 64 MB Ivanushka the Fool lives at the planet of 0-l ...
- poj2367 拓扑序
题意:有一些人他们关系非常复杂,一个人可能有很多后代,现在要制定一种顺序,按照前辈在后代前排列就行 拓扑序裸题,直接建边拓扑排序一下就行了. #include<stdio.h> #incl ...
- 越狱Season 1-Episode 7: Riots, Drills and the Devil: Part 2
Season 1, Episode 7: Riots, Drills and the Devil: Part 2 -Pope: Belick, get those guys in line guy: ...
- AJAX保留浏览历史的解决方案——hashchange()
在ajax请求中,不能更新地址栏,地址栏上的“前进”和“后退”按钮就失效了,带来了另外一种糟糕的用户体验. 解决方案如下: 方案一:使用window. Onhashchange 事件 如下面Html片 ...
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- lua堆栈操作常用函数学习二
/* ** basic stack manipulation */ LUA_API int <strong> (lua_gettop) (lua_State *L); </str ...