using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace conInfo
{
class Program
{
static void Main(string[] args)
{
//要初始設定變數以使其包含產品的相關資訊
string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 1.03;
//為“控制台”視窗 (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("Output.txt"));
Debug.Listeners.Add(tr2);
//將類產生的訊息指定為 WriteLine 方法的第一個輸入參數。按 CTRL+ALT+O 按鍵組合以確保“輸出”視窗可見。
Debug.WriteLine("Debug Information-Product Starting ");
//為了清晰易讀,請使用 Indent 方法在“輸出”視窗中縮排後面的訊息
Debug.Indent();
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());
//為了清晰易讀,請使用 Unindent 方法去除 Debug 類為後續訊息產生的縮排。
//當您將 Indent 和 Unindent 兩種方法一起使用時,讀取器可以將輸出分成組。
Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");
//您還可以使用 WriteLine 方法顯示現有對象的名稱空間和類名稱。
//例如,下面的代碼在“輸出”視窗中顯示 System.Xml.XmlDocument 命名空間
System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();
Debug.WriteLine(oxml);
//要整理輸出,可以包括一個類別作為 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");
//僅在使用 Debug 類的 WriteLineIf 方法將指定條件計算為 true 時,“輸出”視窗才可以顯示訊息。
//將要計算的條件是 WriteLineIf 方法的第一個輸入參數。
//WriteLineIf 的第二個參數是僅在第一個參數的條件計算為真時才顯示的訊息。
Debug.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");
//使用 Debug 類的 Assert 方法,使“輸出”視窗僅在指定條件計算為 false 時才顯示訊息:
Debug.Assert(dUnitCost > 1, "Message will NOT appear");
Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");
//為了確保每個 Listener 對象收到它的所有輸出,請為 Debug 類緩衝區調用 Flush 方法:
Debug.Flush();
}
}
}