Ronnie Overby
Ronnie Overby

Reputation: 46490

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?

Upvotes: 46

Views: 33295

Answers (8)

Dariusz Woźniak
Dariusz Woźniak

Reputation: 10350

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

See also

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

Upvotes: 117

JerryOL
JerryOL

Reputation: 1449

This works in asp.net:

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

from Rick Strahl @ Detecting-ASPNET-Debug-mode

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 2860

The following is safe to use:

var isDebug = false;
#if DEBUG
    isDebug = System.Diagnostics.Debugger.IsAttached;
#endif

if (isDebug) {
    // Do something
}

Upvotes: 2

James Hulse
James Hulse

Reputation: 1581

I had this same problem and the solution I went with is using:

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

This means that technically in production you can attach a debugger and get that piece of code to run.

Upvotes: 16

Edward Leno
Edward Leno

Reputation: 6327

Here is another post with a similar result : http://www.bigresource.com/Tracker/Track-vb-lwDKSoETwZ/

A better explanation can be seen at : 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
    }
}

Upvotes: 5

George Johnston
George Johnston

Reputation: 32278

In addition to #if #endif directives, you can also use conditional attributes. If you mark a method with the attribute

[Conditional("Debug")]

It will only be compiled and run when your application is built in debug mode. As was noted in the comment below, these only work when the method has a void return type.

Upvotes: 7

David_001
David_001

Reputation: 5822

public int Method ()
{
#if DEBUG 
   // do something 
#endif
}

Upvotes: 2

Matt Greer
Matt Greer

Reputation: 62057

yes, wrap the code in

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

Google for "C# compilation symbols"

Visual Studio automatically defines DEBUG when you are in the debug configuration. You can define any symbols you want (look at your project's properties, the build tab). Beware that abusing preprocessor directives is a bad idea, it can lead to code that is very difficult to read/maintain.

Upvotes: 33

Related Questions