Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

how to add debug code? (should go to Debug, shouldn't go to Release)

I need to log a lot of information in my software for debugging. However I need this option only during development, I would prefer to exclude all this code in Release.

Of course I can surround the place I want to debug with "if":

if (isDebugMode()) {
  Logger.log(blahblah);
}

But because my software is pretty time-critical, I want to avoid a lot of unnesseray "if" tests.

I.e. I think I need analog of c "#define #ifdef #ifndef". Are there any technics in c# or .net to solve my task easily?

Upvotes: 4

Views: 1082

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660563

As others have mentioned, you can use a combination of the "preprocessor" directives:

#if DEBUG
    ThisWholeSectionIsTreatedAsACommentIfDEBUGIsNotDefined();      
#endif

and the conditional attribute:

Debug.Assert(x); // Call will be removed entirely in non-debug build because it has conditional attr

However, it is important to realize that one of them is a part of the lexical analysis of the program and the other is part of the semantic analysis. It is easy to get confused. My article on the difference might help:

http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/

Upvotes: 3

Manish Basantani
Manish Basantani

Reputation: 17509

Conditional attribute could help in this case.

Something like this:

class DebugModeLogger
{
    [Conditional("DEBUG")]
    public static void WriteLog(string message)
    {
        //Log it
    }
}

Extract from MSDN: The attribute Conditional enables the definition of conditional methods. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted.

Upvotes: 1

Teoman Soygul
Teoman Soygul

Reputation: 25742

You can use this as a native alternative for debug only logging:

Debug.Write("blah blah");

which will only be logged if this is a debug build.

Upvotes: 2

Blindy
Blindy

Reputation: 67574

You can use [ConditionalAttribute("DEBUG")] in front of your Logger.log method so it will only get compiled in debug mode and completely removed in release mode.

This is how the Debug class methods are all marked.

The upside of this compared to using the #if approach is that code with the Conditional attribute doesn't even exist at runtime, and all references to it get removed, instead of staying there with an empty body (or however you'd compile it conditionally).

Upvotes: 13

Ed Swangren
Ed Swangren

Reputation: 124800

Why don't you instead use the DEBUG symbol?

#if DEBUG
  // debug only code here
#endif

Here is an article describing all of the preprocessor directives available to you in C#, and you are also able to define your own preprocessor variables in your project's properties menu.

Upvotes: 7

Related Questions