C#调试类

1  Debug类

(1)提供一组帮助调试代码的方法和属性。

(2)Debug.Listeners 属性 获取监视调试输出的侦听器集合.Listeners 集合被 DebugTrace 类共用;向任何一个类添加元素侦听器将会向两者同时添加。

 

/* Create a listener that outputs to the console screen, and 
  * add it to the debug listeners. */
 TextWriterTraceListener myWriter = new 
    TextWriterTraceListener(System.Console.Out);
 Debug.Listeners.Add(myWriter);

(3)实例:The following example uses Debug to indicate the beginning and end of a program's execution. The example also uses the Indent and Unindent methods to distinguish the tracing output.

// Specify /d:DEBUG when compiling.

using System;
using System.Data;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
       Debug.AutoFlush = true;
       Debug.Indent();
       Debug.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Debug.WriteLine("Exiting Main"); 
       Debug.Unindent();
    }
}

 

2 TextWriterTraceListener类

(1)将跟踪或调试输出定向到 TextWriterStream,如 FileStream

public class Sample
{

public static int Main(string[] args)
 {
    // Create a file for output named TestFile.txt.
    Stream myFile = File.Create("TestFile.txt");
 
    /* Create a new text writer using the output stream, and add it to
     * the trace listeners. */
    TextWriterTraceListener myTextListener = new 
       TextWriterTraceListener(myFile);
    Trace.Listeners.Add(myTextListener);
 
    // Write output to the file.
    Trace.Write("Test output ");
 

    // Flush the output.
    Trace.Flush(); 

    return 0;
 }

}

3  Trace Class

(1)Provides a set of methods and properties that help you trace the execution of your code. This class cannot be inherited.

(2)Samples:

The following example uses Trace to indicate the beginning and the end of a program's execution. The example also uses the Trace.Indent and Trace.Unindent methods to distinguish the tracing output. For a more complete example of the use of Trace, see How to: Add Trace Statements to Application Code.

// Specify /d:TRACE when compiling.

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
       Trace.AutoFlush = true;
       Trace.Indent();
       Trace.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Trace.WriteLine("Exiting Main"); 
       Trace.Unindent();
    }
}

 

 

 

 

posted @ 2012-05-25 09:14  金河  阅读(1809)  评论(0编辑  收藏  举报