DEBUG&TRACE

來源:互聯網
上載者:User
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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.