1

To Log output I am using the following code.

 System.Diagnostics.Debug.WriteLine("Hello");

Now, its always advisory to remove such logs before the app is submitted.

So, is that we have to remove such lines, before we submit it for release or its done implicitly.

Is there any another better way to log output in C#, which removes the logging to the console when its released. I see Log4Net is one of them.

weber67
  • 525
  • 1
  • 9
  • 18

2 Answers2

2

All methods on the System.Diagnostics.Debug class have the ConditionalAttribute, so under most compilers they will not be compiled into a Release build (unless you define the DEBUG attribute in the release build). 1

This is certainly true for the compilers within Visual Studio.

Your second question about log4Net is actually the reverse, and something to be careful about if you do decide to start using log4Net - log4Net debug calls are included within debug builds and are emitted if you have the logger set to the debug trace level (usually done with runtime configuration).


1. The MSDN pages are actually (IMO) a little bit unclear, but these SO posts agree with my interpretation:

System.Diagnostics.Debug.WriteLine in production code

C# Do Debug statements get compiled out when running in Release mode?

Community
  • 1
  • 1
David Hall
  • 32,624
  • 10
  • 90
  • 127
1

You can use preprocessor directive:

#if DEBUG
    System.Diagnostics.Debug.WriteLine("Hello");
#endif

That line will be skipped when you'll build your application in Release build configuration.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Thanks. But if there are 100 places where I have used, I need to add this directive everywhere. – weber67 Apr 07 '13 at 14:35
  • 1
    Are you certain about this? I've always believed that methods of the Debug class are not compiled into a Release build due to the [ConditionalAttribute]. This post seems to say the same thing http://stackoverflow.com/questions/1600985/system-diagnostics-debug-writeline-in-production-code – David Hall Apr 07 '13 at 14:41
  • @DavidHall thats right. IDE implicitly removes it for release. I did check the document but I am new to this language, so missed out such small thing. – weber67 Apr 07 '13 at 14:55