C# Debug


語法、IDE環境使用、Debug方法是學習一門語言的最少必須技能,本文總結C#中的最常用調試方法

一、 斷點

如下圖所示在欲插入斷點的地方右鍵》斷點》插入斷點(或在行號左邊點擊)可在選中語句上插入斷點:

image_thumb2

Debug模式下程序運行到斷點所在語句時會阻塞在該語句上,如下圖所示

image_thumb3

此時可以通過工具欄上“逐語句”、“逐過程”、“跳出”即image_thumb41來進行調試,調試期間光標放在變量上可以顯示變量的當前值。

二、 跟蹤點

在圖1中,不僅可以插入斷點,也可插入跟蹤點,跟蹤點是一種特殊的斷點,可以配置為滿足一定條件后才命中該斷點,並可以將重要信息輸出到Output窗口,類似於Debug.WriteLine(),它實際上是輸出調試信息且不修改代碼的一種方式。

image_thumb4

根據配置不同跟蹤點有如下三種形狀,從上到下依次為

  • 輸出信息並阻塞
  • 輸出信息不阻塞
  • 條件性阻塞並輸出信息

image_thumb5

三、 Debug與Trace類記錄調試信息

//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窗口

System.Diagnostics.Debug.Assert(true, "info");

System.Diagnostics.Debug類與System.Diagnostics.Trace與Log4Net相對比,使用非常簡單,並且高度可視化實時監測。如果使用Log4Net等日志框架,需要進行各種繁雜的配置,不花上幾天時間難以掌握這套框架。雖然Log4Net等日志框架功能強大,但是目前為止我需要的功能System.Diagnostics.Debug類與System.Diagnostics.Trace完全能滿足。

3.1 配置監聽器(Listeners)

Debug與Trace的監聽器(Listeners)是可以自定義的,以下是MSDN原文描述:

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>


3.2 利用DebugView監聽調試信息

在生成exe后,直接雙擊exe運行,則Debug與Trace輸出的調試信息可以直接在DebugView中看到。在DebugView可以通過filter查看自己感興趣的調試信息(你可能會看到很多程序都在向這里寫入調試信息)。

image_thumb7

軟件捕獲的是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

3.3 System.Diagnostics.Debug類與System.Diagnostics.Trace類的區別

3.3.1 條件編譯符

開發產品的時候,為了追蹤代碼運行狀態我們會加入一些用於輸出調試信息的代碼,如System.Diagnostics.Debug.WriteLine("info")、Console.WriteLine等。但是產品發布的時候,我們不需要這些調試信息和調試代碼,這就需要通過條件編譯符來實現。即在debug模式和release模式下選擇性編譯某些代碼。條件編譯符另外一個最常見到的地方是跨平台程序的代碼中,即根據平台不同條件性的編譯不同的代碼。

C#中通過

· 給方法加上ConditionalAttribute特性來控制代碼的編譯

· 使用#if..#else..#endif,來控制代碼的編譯

來實現條件編譯

3.3.2 Debug類與Trace類區別

Debug類與Trace類的差別就在於條件編譯符不同,Debug類必須在定義了名為“DEBUG”的宏的條件下才會被編譯,而Trace必須在定義了名為“TRACE”的宏的條件下才會被編譯,而默認條件下VS在debug模式下會定義“DEBUG”與“TRACE”宏,在release模式下只定義“TRACE”宏。因此就可以理解Debug類只能在Debug模式下執行,在Release模式下無效果,而Trace類在Debug和Release模式下都可以執行。Debug類只能在Debug模式下執行,在Release模式下無效果,而Trace類在Debug和Release模式下都可以執行。image_thumb9


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM