What is the difference between Console.WriteLine () and Debug.WriteLine ()?

What is the difference between Console .WriteLine() vs Debug .WriteLine() ?

+67
Jun 10 2018-10-10T00:
source share
3 answers

Console.WriteLine is written to the standard output stream, either in debugging or in release. Debug.WriteLine writes to trace listeners in Listeners , but only when working in debugging. When an application compiles in a release configuration, Debug elements will not be compiled into code.

As Debug.WriteLine records all trace listeners in the Listeners collection, it is possible that it can be displayed in more than one place (Visual Studio output window, console, log file, third-party application that registers the listener (I believe DebugView does this) and etc.).

+75
Jun 10 2018-10-10T00:
source share

Console.WriteLine() intended for console mode programs. A good feature of the Visual Studio hosting process is to display it in the Visual Studio output window when debugging for processes that do not have a console. This is very useful when debugging, but be careful that you must remove this code (or wrap it with #ifdef DEBUG ) when you are ready to create the Release assembly. Otherwise, this will add unnecessary overhead to your program. This makes it less ideal for debugging tracking.

Debug.WriteLine() generates trace information if you create conditional #defined using DEBUG . This is enabled by default in the Debug assembly. If the output ends, you can configure it in the app.exe.config file. If this configuration is not overridden, .NET automatically provides an instance of the DefaultTraceListener class. It sends the text Debug.WriteLine() using the Windows API function OutputDebugString() for the debugger. The Visual Studio debugger does this in the output window, as does Console.WriteLine() .

The Debug.WriteLine() advantage of Debug.WriteLine() is that it does not generate overhead in the Release assembly, calls are effectively deleted. However, it does not support compound formatting, for this you will need String.Format() . To track debugging, the Debug class should be your choice.

+27
Jun 10 2018-10-10T00:
source share

If your goal of using Console.WriteLine is for debugging only, you better use Debug.WriteLine .

If you want to show the message to your user, you should use Console.WriteLine .

Debug.WriteLine is only for debugging your application. In release mode, your debug statements will be ignored.

Another use of the console application is to check private assemblies. Instead of the traditional approach to creating a kind of test harness for testing the compiled version of the DLL, you can simply rebuild the DLL as a console application and input / output from / to the console. I found this technique faster than wasting time creating a test GUI harness.

+14
Jun 10 2018-10-10T00:
source share



All Articles