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);
Here's the
ModeDetector
class:
using System;
namespace SoftwareMonkeys.SiteStarter.Diagnostics
{
public class ModeDetector
{
public virtual bool IsDebug
{
get {
bool isDebug = false;
#if (DEBUG)
isDebug = true;
#else
isDebug = false;
#endif
return isDebug;
}
}
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.