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();
        }
    }
  • 相关阅读:
    哈希表及其应用分析
    程序员常用的查找算法
    程序猿必备排序算法及其时间复杂度分析
    递归和回溯求解8皇后问题
    链表种类及其常用操作
    为什么要使用稀疏矩阵??
    微服务项目持续集成部署流程简介
    微服务项目的docker自动化部署流程
    (高考标准分)数据拟合==>多项式方程==>excel公式算成绩(标准分)
    awk用名称对应关系批量重命名
  • 原文地址:https://www.cnblogs.com/wang7/p/2517464.html
Copyright © 2011-2022 走看看