【问题标题】:Need .NET code to execute only when in debug configuration仅在调试配置中需要执行 .NET 代码
【发布时间】:2010-08-04 20:33:42
【问题描述】:

我有一些代码可以访问网络上的 API。 API 的参数之一允许我让他们知道我正在测试。

我只想在测试时在我的代码中设置这个参数。目前,我只是在进行发布构建时将代码注释掉。

是否有一种基于构建配置的自动方法?

【问题讨论】:

    标签: c# .net vb.net visual-studio debugging


    【解决方案1】:

    解决方案

    您可以使用以下方法之一——

    1:Conditional 属性

    Conditional 属性向编译器指示应忽略方法调用或属性,除非定义了指定的条件编译符号。

    代码示例:

    [Conditional("DEBUG")]
    static void Method() { } 
    

    1b: Conditional 本地函数的属性 (C# 9)

    从 C# 9 开始,您可以在本地函数上使用属性。

    代码示例:

    static void Main(string[] args)
    {
        [Conditional("DEBUG")]
        static void Method() { }
    
        Method();
    }
    

    2:#if预处理指令

    当 C# 编译器遇到 #if preprocessor directive,最后是 #endif 指令时,它仅在定义了指定符号的情况下编译指令之间的代码。与 C 和 C++ 不同,您不能将数值分配给符号。 C# 中的#if 语句是布尔值,仅测试符号是否已定义。

    代码示例:

    #if DEBUG
        static int testCounter = 0;
    #endif 
    

    3:Debug.Write 方法

    Debug.Write(和Debug.WriteLine)将有关调试的信息写入Listeners 集合中的跟踪侦听器。

    另请参阅 Debug.WriteIfDebug.WriteLineIf

    代码示例:

    Debug.Write("Something to write in Output window.");
    

    注意事项

    小心使用#if 指令,因为它会在非调试(例如发布)构建中产生意外情况。例如,参见:

        string sth = null;
    #if DEBUG
        sth = "oh, hi!";
    #endif
        Console.WriteLine(sth);
    

    在这种情况下,非调试版本将打印一条空白消息。但是,这可能会在不同的情况下引发NullReferenceException

    了解更多

    另见

    还有一个工具,DebugView,它允许从外部应用程序捕获调试信息。

    【讨论】:

    • 那些Debug.WriteXXX 方法也归于[Conditional("DEBUG")]
    • 感谢您在过去 10 年中不断更新此答案 :)
    【解决方案2】:

    是的,将代码包装在

    #if DEBUG
    // do debug only stuff 
    #else
    // do non DEBUG stuff
    #endif
    

    谷歌"C# compilation symbols"

    当您处于调试配置中时,Visual Studio 会自动定义 DEBUG。您可以定义任何您想要的符号(查看项目的属性,构建选项卡)。请注意,滥用预处理器指令是一个坏主意,它可能会导致代码非常难以阅读/维护。

    【讨论】:

    • 你能举个滥用的例子吗?
    • 我曾经参与过一个项目,其中 #if VERSION_1_1 // do version 1.1 specific stuff #elif VERSION_2_0 #if INCLUDE_FLASH // do flash specific stuff #elif HTML_RENDERER // HTML specific stuff #else /// etc #endif 当然是一个格式不正确的评论,但你明白了。这就是他们决定维护不同版本的方式。这足以让你想去邮局。
    • 啊。谢谢你的建议。我不想做那样的事情。我认为在这种情况下使用源代码控制分支可能会更好。
    【解决方案3】:

    我遇到了同样的问题,我使用的解决方案是:

    if (System.Diagnostics.Debugger.IsAttached)
    {
        // Code here
    }
    

    这意味着从技术上讲,在生产环境中,您可以附加一个调试器并让这段代码运行。

    【讨论】:

      【解决方案4】:

      除了#if #endif 指令之外,您还可以使用条件属性。如果你用属性标记一个方法

      [Conditional("Debug")]
      

      只有在您的应用程序以调试模式构建时才会编译和运行。正如下面评论中所指出的,这些仅在方法具有 void 返回类型时才有效。

      【讨论】:

        【解决方案5】:

        这是另一个类似结果的帖子:http://www.bigresource.com/Tracker/Track-vb-lwDKSoETwZ/

        更好的解释可见:http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

        // preprocessor_if.cs
        #define DEBUG
        #define MYTEST
        using System;
        public class MyClass 
        {
            static void Main() 
            {
        #if (DEBUG && !MYTEST)
                Console.WriteLine("DEBUG is defined");
        #elif (!DEBUG && MYTEST)
                Console.WriteLine("MYTEST is defined");
        #elif (DEBUG && MYTEST)
                Console.WriteLine("DEBUG and MYTEST are defined");
        #else
                Console.WriteLine("DEBUG and MYTEST are not defined");
        #endif
            }
        }
        

        【讨论】:

          【解决方案6】:
          public int Method ()
          {
          #if DEBUG 
             // do something 
          #endif
          }
          

          【讨论】:

            【解决方案7】:

            以下内容可以安全使用:

            var isDebug = false;
            #if DEBUG
                isDebug = System.Diagnostics.Debugger.IsAttached;
            #endif
            
            if (isDebug) {
                // Do something
            }
            

            【讨论】:

              【解决方案8】:

              这适用于 asp.net:

              if (System.Web.HttpContext.Current.IsDebuggingEnabled)
                  //send email to developer;
              else
                  //send email to customer;
              

              来自 Rick Strahl @Detecting-ASPNET-Debug-mode

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-10-30
                • 2018-11-27
                • 2013-02-23
                • 2017-04-13
                相关资源
                最近更新 更多