• C# DEBUG 调试信息打印及输出详解


    转载自: http://blog.csdn.net/aaaaatiger/article/details/5583301

    1.debug只在[debug模式下才执行](运行按钮后面的下拉框可选)

    2.断言 [Assert]

    System.Diagnostics.Debug.Assert(false,"信息"); 

    将出现一个对话框,类似下图:

     

    3.debug可以自定义监听器
    (下例将信息存入磁盘文件)

    System.Diagnostics.TextWriterTraceListener TraceListener = new System.Diagnostics.TextWriterTraceListener(@"d:/debug.txt");
    System.Diagnostics.Debug.Listeners.Add(TraceListener);
    System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());
    Debug.WriteLine("Hello, world!");
    TraceListener.Flush();

     

    4. debug和console.write()有什么区别?
    debug在运行状态时向ide的限时窗口输出(用于windows 窗体程序)
    console.write用于控制台程序,使用程序在运行时可以向控制台(就是dos界面的那个)输出信息
    二者同样是输入,但Debug是输出到output窗口,而Console是输出到控件台窗口,
    而且Debug必须要在Debug情况下才有效,你按Ctrl+F5后会看到Console的输出,
    按F5后也能看到Console的输出,还可以看到output中Debug的输出

    5  条件编译 [#if DEBUG ]
    首先,大小写不能写错,其次,解决方案配置设为:Debug,才会执行该语句,如果在条件里面搭配Debug.Assert等,效果甚佳。而如果要设置为Release模式,就不会执行条件语句中的内容,有时候可以通过设置!DEBUG来达到发布产品执行的代码。

    示例代码:

    int debugNumber = 0;
    
    #if DEBUG 
    Console.WriteLine("调试中的debugNumber: "+debugNumber);
    #endif
    
    #if !DEBUG
    debugNumber++;
    Console.WriteLine("非调试中的debugNumber: "+debugNumber);
    #endif

    在不同的调试方式下(Debug和Release),执行输出的内容是不一样的。

    利用宏定义 [#define]  [#undef DEBUG]

    #define DEBUG// C#的宏定义必须出现在所有代码之前。当前我们只让DEBUG宏有效。
    using System.Diagnostics; //必须包含这个包

    解决方案处于 Debug 模式

    #define ZHANG
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                ToolKit.Method1();
                ToolKit.Method2();
                ToolKit.Method3();
                ToolKit.Method4();
            }
    
            class ToolKit
            {
                [ConditionalAttribute("LI")]// Attribute名称的长记法
                [ConditionalAttribute("DEBUG")]
                public static void Method1()
                { Console.WriteLine("Created By Li, Buged.11"); }
    
                [ConditionalAttribute("LI")]
                [ConditionalAttribute("NOBUG")]
                public static void Method2() 
                { Console.WriteLine("Created By Li, NoBug."); }
    
                [Conditional("ZHANG")]// Attribute名称的短记法
                [Conditional("DEBUG")]
                public static void Method3()
                { Console.WriteLine("Created By Zhang, Buged.11"); }
    
                [Conditional("ZHANG")]
                [Conditional("NOBUG")]
                public static void Method4()
                { Console.WriteLine("Created By Zhang, NoBug."); }
            }
        }
    }

    运行结果:

  • 相关阅读:
    【Mybatis源码解析】- JDBC连接数据库的原理和操作
    【JDK源码解析】- ArrayList源码解析,绝对详细
    【设计模式】-代理模式及动态代理详解
    【Java基础】反射机制及应用
    Go 中的 channel 与 Java BlockingQueue 的本质区别
    Github Actions 还能做这些事
    写了一个 gorm 乐观锁插件
    Go 去找个对象吧
    Web 自动化测试全面提升之 Pytest
    【51testing专访】web自动化,从入门到进阶
  • 原文地址:https://www.cnblogs.com/gsk99/p/4935933.html
Copyright © 2020-2023  润新知