语法、IDE环境使用、Debug方法是学习一门语言的最少必须技能,本文总结C#中的最经常使用调试方法html
以下图所示在欲插入断点的地方右键》断点》插入断点(或在行号左边点击)可在选中语句上插入断点:框架
Debug模式下程序运行到断点所在语句时会阻塞在该语句上,以下图所示工具
此时能够经过工具栏上“逐语句”、“逐过程”、“跳出”即来进行调试,调试期间光标放在变量上能够显示变量的当前值。学习
在图1中,不只能够插入断点,也可插入跟踪点,跟踪点是一种特殊的断点,能够配置为知足必定条件后才命中该断点,并能够将重要信息输出到Output窗口,相似于Debug.WriteLine(),它其实是输出调试信息且不修改代码的一种方式。spa
根据配置不一样跟踪点有以下三种形状,从上到下依次为.net
//System.Diagnostics.Debug类与System.Diagnostics.Trace可用于记录并输出调试信息
System.Diagnostics.Debug.Write("info"); System.Diagnostics.Debug.WriteIf(true, "info"); System.Diagnostics.Debug.WriteLine("info"); System.Diagnostics.Debug.WriteLineIf(true, "info");
//将info记录到监听器和VS的Output窗口debug
System.Diagnostics.Debug.Assert(true, "info");调试
System.Diagnostics.Debug类与System.Diagnostics.Trace与Log4Net相对比,使用很是简单,而且高度可视化实时监测。若是使用Log4Net等日志框架,须要进行各类繁杂的配置,不花上几天时间难以掌握这套框架。虽然Log4Net等日志框架功能强大,可是目前为止我须要的功能System.Diagnostics.Debug类与System.Diagnostics.Trace彻底能知足。日志
Debug与Trace的监听器(Listeners)是能够自定义的,如下是MSDN原文描述:code
The listeners produce formatted output from the debug output. By default, the collection contains an instance of the DefaultTraceListener class. To remove the default listener, call the Remove method, and pass it the instance of the DefaultTraceListener. To redirect output to the console window, add an instance of the ConsoleTraceListener. To redirect output to a file or stream, add an instance of the TextWriterTraceListener. The Listeners collection is shared by both the Debug and the Trace classes; adding a trace listener to either class adds the listener to both.
具体添加一个Listener方法以下:
/// <summary> /// 添加控制台监听器 /// 添加该监听器后程序运行期间将会跳出控制台窗口显示调试信息 /// </summary> public static void AddConsoleTraceListener(bool useErrorStream) { ConsoleTraceListener t = new ConsoleTraceListener(useErrorStream); System.Diagnostics.Debug.Listeners.Add(t); System.Diagnostics.Debug.AutoFlush = true; } /// <summary> /// 添加日志文件监听器 ///添加该监听器后程序运行时会将调试信息写入exe所在目录的.log文件中 /// </summary> public static void AddTextWriterTraceListener() { TextWriterTraceListener t = new TextWriterTraceListener(FileName); System.Diagnostics.Debug.Listeners.Add(t); System.Diagnostics.Debug.AutoFlush = true; }
实际上也能够经过配置文件添加与配置
<configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" /> <remove name="Default" /> </listeners> </trace> </system.diagnostics> </configuration>
在生成exe后,直接双击exe运行,则Debug与Trace输出的调试信息能够直接在DebugView中看到。在DebugView能够经过filter查看本身感兴趣的调试信息(你可能会看到不少程序都在向这里写入调试信息)。
该软件捕获的是exe直接运行时,抛出的信息,而不是Visual Studio调试时的,所以只适合在你的软件已经部署之后用来查看软件运行中抛出的调试信息,至关于VS Output窗口替代品。
关于DebugView详细介绍能够参考
DebugView调试入门篇
http://blog.csdn.net/jiankunking/article/details/44984487
https://www.cnblogs.com/hbccdf/p/csharp_debug_induction.html
DebugView下载地址
https://docs.microsoft.com/zh-cn/sysinternals/downloads/debugview
开发产品的时候,为了追踪代码运行状态咱们会加入一些用于输出调试信息的代码,如System.Diagnostics.Debug.WriteLine("info")、Console.WriteLine等。可是产品发布的时候,咱们不须要这些调试信息和调试代码,这就须要经过条件编译符来实现。即在debug模式和release模式下选择性编译某些代码。条件编译符另一个最多见到的地方是跨平台程序的代码中,即根据平台不一样条件性的编译不一样的代码。
C#中经过
· 给方法加上ConditionalAttribute特性来控制代码的编译
· 使用#if..#else..#endif,来控制代码的编译
来实现条件编译
Debug类与Trace类的差异就在于条件编译符不一样,Debug类必须在定义了名为“DEBUG”的宏的条件下才会被编译,而Trace必须在定义了名为“TRACE”的宏的条件下才会被编译,而默认条件下VS在debug模式下会定义“DEBUG”与“TRACE”宏,在release模式下只定义“TRACE”宏。所以就能够理解Debug类只能在Debug模式下执行,在Release模式下无效果,而Trace类在Debug和Release模式下均可以执行。Debug类只能在Debug模式下执行,在Release模式下无效果,而Trace类在Debug和Release模式下均可以执行。