程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 如何在Visual C#.NET中跟蹤和調試(4)

如何在Visual C#.NET中跟蹤和調試(4)

編輯:關於C語言

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 名稱空間。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved