views:

239

answers:

6

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?

+20  A: 

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.

Matt Greer
Can you give an example of abuse?
Ronnie Overby
I once worked on a project that had`#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`of course being a comment that doesn't format correctly, but you get the idea.This was how they decided to maintain different releases. It was enough to make you want to go postal.
Matt Greer
Ahh. Thanks for the advice. I'm not trying to do anything like that. I think branching with source control would probably be better in situations like that.
Ronnie Overby
+1  A: 
public int Method ()
{
#if DEBUG 
   // do something 
#endif
}
David_001
+4  A: 

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.

George
This only works on methods that return `void`, btw.
Matt Greer
+1  A: 

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
    }
}
Edward Leno
+17  A: 

You can use Conditional attribute:

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

#if preprocessor directive:

#if DEBUG
    static int testCounter = 0;
#endif 

Or Debug.Write method:

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

Beware of using #if directive since it can produce unintended situations in Release build. For example, see:

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

In this case Release build will print a blank message.

Read more:

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

ventr1s
+1  A: 

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.

James Hulse