語法、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模式下均可以執行。