zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    人间故事馆话题:聊聊那些被骗经历,让其他人不再被骗
    路过的风景
    路过的风景
    上海最适合拍照的旅游地点
    Java EE (11)
    五、服务器端的局域网
    P1294 高手去散步 洛谷
    堆排序【模板】
    P3383 【模板】线性筛素数 洛谷
    P1516 青蛙的约会 洛谷
  • 原文地址:https://www.cnblogs.com/wang7/p/2517464.html
Copyright © 2011-2022 走看看