首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Visual C#.NET 追蹤與偵錯 (2):分類輸出、條件式 WriteLineIf/Assert、TraceListener 雙通道配置

     編輯:關於C語言

分類輸出 (Category 參數)

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");

輸出格式:Category: Message (例:Field: The product name is Widget)。

WriteLineIf 條件式輸出

Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");

第一參數為條件,第二參數為條件 true 時輸出的訊息。

Assert 斷言輸出 (條件 false 才輸出)

Debug.Assert(dUnitCost > 1, "Message will NOT appear");
Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");

雙通道 TraceListener 配置

TextWriterTraceListener tr1 = new TextWriterTraceListener(Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2);

tr1 導向主控台,tr2 導向檔案。

Indent/Unindent 與 Flush

Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");
Debug.Flush();
  • Indent/Unindent 搭配產生巢狀縮排,便於閱讀。
  • Flush() 強制將緩衝區資料寫入所有 Listener。

Trace 類別共用方法

Trace 與 Debug 共享:WriteLine、WriteLineIf、Indent、Unindent、Assert、Flush。差異在條件編譯符號 (DEBUG vs TRACE)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved