Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#
Tip/Trick

(C#) Determining whether the current build mode is Debug or Release

Rate me:
Please Sign up or sign in to vote.
2.17/5 (5 votes)
4 Feb 2012CPOL 58.5K   2   9
Sometimes, it's useful to know whether you're running in debug or release mode so you can do extra work during debugging and skip it during release. This class makes that as simple as possible.
Often when debugging, it's good to do things like output extra information, etc. which you want to skip when the application is compiled in release mode.

The most obvious example is logging, which is more detailed in debugging, and should output as little as possible when released live.

Here's how the ModeDetector class works:

C#
if (new ModeDetector().IsDebug)
  PerformDebugLogging(message);
// else
// skip it because the application is compiled in Release mode and we want to optimize performance


Here's the ModeDetector class:

C#
using System;

namespace SoftwareMonkeys.SiteStarter.Diagnostics
{
        /// <summary>
        /// Used to detect the current build mode.
        /// </summary>
        public class ModeDetector
        {
                /// <summary>
                /// Gets a value indicating whether the assembly was built in debug mode.
                /// </summary>
                public virtual bool IsDebug
                {
                        get {
                                bool isDebug = false;

                                #if (DEBUG)
                                isDebug = true;
                                #else
                                isDebug = false;
                                #endif
                                
                                return isDebug;
                        }
                }
                
                /// <summary>
                /// Gets a value indicating whether the assembly was built in release mode.
                /// </summary>
                public bool IsRelease
                {
                        get { return !IsDebug; }
                }
        }
}


http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/SoftwareMonkeys.SiteStarter.Diagnostics/ModeDetector.cs[^]


One reason for using this ModeDetector class is so that a mock version can be used during testing. Here's an example of the mock version:
http://code.google.com/p/sitestarter/source/browse/trunk/Src/App/SoftwareMonkeys.SiteStarter.Diagnostics.Tests/MockModeDetector.cs[^]

Instead of having to recompile in debug mode just to test the debug logging, the MockModeDetector can be used to simulate debug mode regardless of the actual compilation mode.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
SoftwareMonkeys
Australia Australia
Founder of www.softwaremonkeys.net

Comments and Discussions

 
Questionmake static? Pin
baker_tony19-Jun-13 16:06
baker_tony19-Jun-13 16:06 
GeneralRe: I think it simply should be - #if DEBUG PerformDebugLo... Pin
Pankaj Chamria30-Jan-12 17:35
Pankaj Chamria30-Jan-12 17:35 
GeneralReason for my vote of 1 I am voting 1. As this is waste of c... Pin
senny7-Feb-12 5:16
senny7-Feb-12 5:16 
GeneralReason for my vote of 2 guessing Pin
johannesnestler1-Feb-12 4:26
johannesnestler1-Feb-12 4:26 
GeneralJust a question: What is if code was build with "debug" info... Pin
johannesnestler1-Feb-12 4:22
johannesnestler1-Feb-12 4:22 
GeneralReason for my vote of 2 The reason it is bad design is becau... Pin
Pankaj Chamria30-Jan-12 3:13
Pankaj Chamria30-Jan-12 3:13 
GeneralRe: The performance hit of the code that gets executed (which is... Pin
SoftwareMonkeys30-Jan-12 10:18
SoftwareMonkeys30-Jan-12 10:18 
GeneralReason for my vote of 1 It's bad pattern. Pin
Member 13192329-Jan-12 23:06
Member 13192329-Jan-12 23:06 
GeneralRe: Can you please explain why? And explain the alternatives for... Pin
SoftwareMonkeys29-Jan-12 23:10
SoftwareMonkeys29-Jan-12 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.