Conditional compilation in C#

       Programmers need to debug, which sometimes requires identification of points in your program where a programmer would like to insert code that would help him/her to debug his/her code efficiently. A simple example might be inserting a Console.Writeline() call that prints out values or indicates completion (successful or unsuccessful) of the executed part. However, these lines can clutter up the code structure and also needs removal of the debugging code for the release of the entire software.

             This overhead is taken care of by specialized methods in C# that help the programmer debug the code without the need to clean up his/her debugging code for the release phase. These methods which are used for debugging are called Conditional methods. The compiler identifies these marked methods and never includes them in the release build.

             Well, that’s good enough. But, how do I make a method conditional? .NET provides an attribute System.Diagnostics.ConditionalAttribute (alias Conditional) to achieve this. Let’s look at some code now.

  Defining the conditional method

 public class MyTracer
{
    [Conditional("DEBUG")]
    public static void LogThisMessage(string myMessage, int severity)
    {
        //  Write message to screen or a file or ...
        Console.WriteLine(" DEBUG MESSAGE : " + myMessage
+" SEVERITY LEVEL : "+severity);
    }
}

 

The Conditional attribute has been applied to the LogThisMessage() method with the DEBUG conditional compilation symbol. This signals the compiler that the method should be ignored if the conditional symbol DEBUG is not specified.

 

Using the conditional method

 With the Solution Configurations set to Debug mode if you execute the following code, you would happily see the output window shown below.

 

public static void Main(string[] args)
{
    //  Some error occured in my code
    //  Log the message
    MyTracer.LogThisMessage("The error 1221 has occured.", 5);
}

 DEBUG on

 

Now lets see what happens to our conditional code in the Release mode. To enable Debug or Release mode you have to look for the Solution Configurations selector in your Visual Studio IDE which is shown below.

 

Change the mode to “Release” and you would see that the conditional code now has disappeared.

 

 

We used the predefined DEBUG compilation symbol in our code. You can have your own defined symbols and use them for conditional compilation. You can define custom compilation symbols in Project Properties -> Build tab -> General. Checkboxes have been provided to enable or disable the DEBUG and TRACE symbols.

 

 

.NET also provides two classes that provide similar functionality:

  • System.Diagnostics.Debug
  • System.Diagnostics.Trace

 These classes contain methods that can also be used for debugging if you do not need more specific custom methods.

posted @ Friday, October 31, 2008 6:55 PM

Print

Comments on this entry:

# re: Conditional compilation in C#

Left by Sudarsan Srinivasan at 11/3/2008 3:01 PM
Gravatar
That was really a nice post. I always wanted to have such a feature for all my applications. Cool.

I have two doubts,

1. What happens if I wanna debug from from client's site where I can;t expect them to have Visual Studio ??

2. In the first question I shall be seeing the logs if i run it from cmd prompt (Not sure !!). Are there any other ways to open a terminal in the BG when the application is opened in debug mode and the terminal should be logging all the messages ??

# re: Conditional compilation in C#

Left by Elroy at 11/3/2008 6:02 PM
Gravatar
Thanks Sudarsan.

1. AFAIK you would provide release builds to your clients, in which case you would not be able to see any of the conditionally compiled code as this kind of code never makes it to the release build.

2. Not in the release build. But, what you are suggesting here may be called a classic case of Inter-Process Communication (IPC), which in conjunction with conditional compilation may provide a lot of help while debugging loads of code.

# re: Conditional compilation in C#

Left by Sudarsan Srinivasan at 11/3/2008 7:21 PM
Gravatar
>>Not in the release build. But, what you are suggesting here may be called a classic case of Inter-Process Communication (IPC), which in conjunction with conditional compilation may provide a lot of help while debugging loads of code.

That will be a big process. There should be some simple way to do it. If you find anything ping it to me.

# re: Conditional compilation in C#

Left by Sudarsan Srinivasan at 11/4/2008 2:22 PM
Gravatar
Eager to check your snippets I was trying out whatever you have mentioed and I came across a loads of other classes too. Check my link out, http://codelog.blogial.com/2008/11/04/tracing-in-c/

Regarding the comment note #3, there are a few solutions. One of which is to change the propery of a WindowApplication to Console so that it runs in CommandPrompt when run in Visual Studio there by enabling you to see the traces. Check this out, http://stackoverflow.com/questions/261054/how-to-see-the-trace-in-a-seperate-console

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678