When working with a WinForms or a WPF application in .NET, a console application is not created by default, so statements involving the Console class normally go to the trash. The console window may be a useful debugging aid, printing anything that may be important during runtime.
Fortunately, there is a way to get it back. Actually, there are two ways. The first, the “hard way” is to create the console explicitly using the native AllocConsole function:
private static extern void AllocConsole();
All that’s left is to call this function in Main, or the Application class’ constructor.
The other way (the easy way) is to simply go to Project Properties and (in the Application tab) change the output type to a console application. That’s it!
To use the console, you can call the usual Console.Write(Line), but there may be a better way, to integrate with the default tracing mechanism of .NET using the Trace class (and friends). The Trace object writes to a collection of TraceListeners, which can be configured easily in the config file.
The following example adds the console as a trace listener:
<add name="console" type="System.Diagnostics.ConsoleTraceListener" />
Now every call to Trace.Write(Line) will also trace to the console (and the default trace listener – the debugger output window – if the debugger is active. Can be captured by the DebugView tool).