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






2.17/5 (5 votes)
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:
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:
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.