简体   繁体   中英

Remove Log before release System.Diagnostics

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM