9.“控制台”窗口和 Output.txt 文件應顯示以下輸出:
The product name is Widget
The available units on hand are 100
The per unit cost is 1.03
Debug Information-Product Ending
Trace Information-Product Starting
The product name is Widget
FIEld: The product name is Widget
This message WILL appear
Trace Information-Product Ending
注意:Output.txt 文件與 conInfo 可執行文件 (conInfo.exe) 位於同一目錄中。通常情況下,該目錄是存儲項目源的 \bin 文件夾,默認情況下為 C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin。
完整代碼列表
using system;
using system.Diagnostics;
class Class1
{
[STAThread]
static void Main(string[] args)
{
string sProdName = "Widget";
int iUnitQty = 100;
double dUnitCost = 1.03;
Debug.WriteLine("Debug Information-Product Starting ");
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());
system.Xml.XmlDocument oxml = new system.Xml.XMLDocument();
Debug.WriteLine(oXML);
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(iUnitQty > 50, "This message WILL appear");
Debug.WriteLineIf(iUnitQty < 50, "This message will NOT appear");
Debug.Assert(dUnitCost > 1, "Message will NOT appear");
Debug.Assert(dUnitCost < 1, "Message will appear since dUnitcost < 1 is false");
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);
Debug.WriteLine("The product name is "+sProdName);
Debug.WriteLine("The available units on hand are"+iUnitQty);
Debug.WriteLine("The per unit cost is "+dUnitCost);
Debug.Unindent();
Debug.WriteLine("Debug Information-Product Ending");
Debug.Flush();
Trace.WriteLine("Trace Information-Product Starting ");
Trace.Indent();
Trace.WriteLine("The product name is "+sProdName);
Trace.WriteLine("The product name is"+sProdName,"FIEld" );
Trace.WriteLineIf(iUnitQty > 50, "This message WILL appear");
Trace.Assert(dUnitCost > 1, "Message will NOT appear");
Trace.Unindent();
Trace.WriteLine("Trace Information-Product Ending");
Trace.Flush();
Console.ReadLine();
}
}
注意:要使此代碼示例發揮作用,必須通過將 using system.Diagnostics; 粘貼為第一行代碼來添加 system.Diagonstics 名稱空間。