Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need .NET code to execute only when in debug configuration

I have some code that access an API out on the web. One of the API's parameters allows me to let them know that I am testing.

I would like to only set this parameter in my code when I am testing. Currently, I just comment the code out when I do a release build.

Is there an automatic way of doing this based on the build configuration?

like image 200
Ronnie Overby Avatar asked Aug 04 '10 20:08

Ronnie Overby


People also ask

How do I run a .NET in debug mode?

NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu. Select the Terminal tab to see the "What is your name?" prompt that the program displays before waiting for a response.

How do I skip the code while debugging in Visual Studio?

You can also click on the line you want to skip to and hit Ctrl+F10 (Run to Cursor).

How do I run code in debug mode?

Press F5 and hover over the type variable again. Repeat this step until you see a value of I in the type variable. Now, press F11 (Debug > Step Into or the Step Into button in the Debug Toolbar). F11 advances the debugger (and executes code) one statement at a time.


1 Answers

Solutions

You can use one of the following—

1: Conditional attribute

The Conditional attribute indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

Code example:

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

1b: Conditional attribute on local function (C# 9)

Since C# 9, you may use attribute on a local function.

Code example:

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

2: #if preprocessor directive

When the C# compiler encounters an #if preprocessor directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

Code example:

#if DEBUG     static int testCounter = 0; #endif  

3: Debug.Write methods

Debug.Write (and Debug.WriteLine) writes information about the debug to the trace listeners in the Listeners collection.

See also Debug.WriteIf and Debug.WriteLineIf.

Code example:

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

Notes

Beware of using #if directive since it can produce unintended situations in non-Debug (e.g. Release) build. For example, see:

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

In this case, non-Debug build will print a blank message. But, this potentially may raise NullReferenceException in a different case.

Read more

  • Eric Lippert. What's the difference between conditional compilation and the conditional attribute?
  • C# Programmer's Reference: Conditional Methods Tutorial (archive.org mirror)
  • Bill Wagner. Effective C#: 50 Specific Ways to Improve Your C# (book), chapter: Use Conditional Attributes Instead of #if
  • John Robbins. Assertions and Tracing in .NET (archive.org mirror)
  • Sam Allen. Dot Not Perls:
    • C# Conditional Attribute
    • C# Debug.Write

See also

There is also a tool, DebugView, which allow to capture debug information from external applications.

like image 154
Dariusz Woźniak Avatar answered Sep 25 '22 06:09

Dariusz Woźniak