4

I use a lot of lines like that

Console.WriteLine("Test");

to debug application under VS 2010.

My question is: Have I do comment all those lines when I build an application?

Thanks!

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    This is suicide to not to use a logging system like log4net. And you are just about to commit one :) – Wiktor Zychla Jul 04 '12 at 17:44
  • You can comment them or use `#if debug`, making sure to switch between Debug and Release while compiling - http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx – JMK Jul 04 '12 at 17:45

4 Answers4

14

Yes. In fact, if your app was a console application, you'd really want those lines executed. Have a look at System.Diagnostics.Debug methods (e.g. Debug.WriteLine) which may be what you need. Their output is in Visual Studio's Output window, and they do nothing in Release code.

More generally, you can have code that's only compiled in a Debug build by doing:

#if DEBUG
// Debug-only code here.
#endif

You can also put this attribute before your method definition to write a method that's not called at all when you do a Release build:

    [System.Diagnostics.Conditional("DEBUG")]

All these methods have the advantage that they shouldn't affect the performance of production code.

To check I'm giving you accurate advice, I compiled the following in Release mode:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello world!");

#if DEBUG
        Console.WriteLine("Inside #if block.");
#endif

        WriteLine("With ConditionalAttribute.");

        Debug.WriteLine("Debug.WriteLine.");
    }

    [Conditional("DEBUG")]
    public static void WriteLine(string line)
    {
        Console.WriteLine(line);
    }
}

I then used the IL Dissasembler tool to see what will actually run:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  ldstr      "Hello world!"
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000a:  ret
} // end of method Program::Main

As you can see, only the Console.WriteLine method is called. The other three alternatives are, as we had hoped, 'compiled out' of the debug code.

The Debug version looks like this:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       46 (0x2e)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "Hello world!"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  ldstr      "Inside #if block."
  IL_0011:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0016:  nop
  IL_0017:  ldstr      "With ConditionalAttribute."
  IL_001c:  call       void ConditionalCompileTest.Program::WriteLine(string)
  IL_0021:  nop
  IL_0022:  ldstr      "Debug.WriteLine."
  IL_0027:  call       void [System]System.Diagnostics.Debug::WriteLine(string)
  IL_002c:  nop
  IL_002d:  ret
} // end of method Program::Main
Olly
  • 5,966
  • 31
  • 60
  • Oh! Last question somehow I cannot see Debug.WriteLine("Debug.WriteLine."); output under VisualStutio output window? How is it possible? – NoWar Jul 04 '12 at 18:08
  • 1
    @Peretz sometimes it's a bit weird and you have to scroll around in the window to see it. – dialer Jul 04 '12 at 18:20
  • @dialer The output window is empty (( How I can fix this issue? – NoWar Jul 04 '12 at 18:26
  • 1
    @dialer I found some help here http://stackoverflow.com/questions/1370449/debug-writeline-not-working and it was helpful. Fixed! ) – NoWar Jul 04 '12 at 18:35
3

Yes, all your Console.WriteLine() will be compiled into IL code and thus embedded into your executable.

Instead of Console.WriteLine() use some logging framework, like log4net or NLog. These are much easier to configure and reuse.

Benefits of logging frameworks over Console.WriteLine

  • A change from debug to production logging will require a one-line update in a config file
  • You can log into file, console, database, xml, you can also send emails and do much more
  • Also you can combine several loggers, like save to file and output to console at the same time
  • How about fancy colour console output - a few lines in a config file
  • Easy to disable (one line change in a config file)
  • Easy to configure and it looks nicer than #if DEBUG .. #endif
  • You can do async logging
  • You don't need to change your logging code if you will split your app into reusable library and UI (for example to use with WPF and not only with Console)
oleksii
  • 35,458
  • 16
  • 93
  • 163
2
#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
1

What type of application is it? In console apps, that's the way to output to the screen. In any case, the answer is yes - it will still be executed. Whether or not there's anything attached to the console is another question.

You may want to look at Debug.WriteLine() but there are may better methods (not to mention Console is very slow in VS)

Basic
  • 26,321
  • 24
  • 115
  • 201