Recently found a comments in the code base that stated "Remove on Live!" in the description. It was perfectly valid code, but unfortunately but there was no way any developer (other than the person who wrote it in the first place) will remove the code before compiling. The good news is that there is a solution to this problem and that’s "Conditional Pre-Compilation Symbols". That all sounds very complicated but its really simple. It means you can mark a section of code, so that it is placed within a specific assembly.
Simple Conditional Pre-Complication
Here is an example of a function that will appear in all environments and assemblies.
//***********************************
// Test code; remove when running on live.
//***********************************
if (X==Y || Z==A)
{
int AnotherX = Convert.ToInt32(X)|
_log.DebugFormat("AnoterX has been set to {0}", X);
}
Pleacing this within a Conditional Pre-Compilation Symbols would look like this:
#if(DEBUG)
//***********************************
// Test code; remove when running on live.
//***********************************
if (X==Y || Z==A
{
int AnotherX = Convert.ToInt32(X)|
_log.DebugFormat("AnoterX has been set to {0}", X);
}
#endif
This means that the code still works fine on you're local PC and Miranda, but now that we're using CC.NET to do a "Release" build, this code will not be included in the assembly.
Doing something more funky
You're not limited to just removing code, you can also directly change the code as shown below:
#if(DEBUG)
_log.DebugFormat("This is Debug Code");
#else
_log.DebugFormat("This is Release Code");
#endif
You are also not limited to only using the pre-configured "DEBUG" or "RELEASE" variables, you can also set your own. To do this, open the property page for your project and select the Build tab. Here you can see a field called "Conditional compilation symbols". Here you can enter a value like "Design", but you should also note the Configuration currently selected as this is important.
Now in your code you can do this….
#if(Design)
_log.DebugFormat("This is only run when Design is entered in the properties");
#endif
You can of course add as many build configurations as you wish, e.g. “Debug”, “Test”, “Stage”, “Live” etc. and CC.NET will happy ensure that each is built in turn correctly.