To not having spoiled the client code with #if DEBUG, you might use the following:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Debugging.DebugSetupConsole();
Application.Run(new FormMain());
}
finally
{
Debugging.DebugCleanupConsole();
}
}
and the Debugging class:
public abstract partial class Debugging
{
private abstract partial class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int FreeConsole();
}
private static bool _consoleOk = false;
[Conditional("DEBUG")]
public static void DebugSetupConsole()
{
_consoleOk = NativeMethods.AllocConsole() == 0;
Console.WriteLine("Debug Console");
}
[Conditional("DEBUG")]
public static void DebugCleanupConsole()
{
if (_consoleOk)
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
NativeMethods.FreeConsole();
}
}
}