I usually send various debug messages to .txt files or the standard console, but I somehow got tired of this DOS feeling... Interactive functions like saving the whole output to a file with one click or easily clearing the list are not available as well. So I started writing this dialog console. The
DebugConsole class is a singleton that has the
Form class as member, this seemed the best way in C# to have this class acting like a global variable.
The first version didn't have any support for the listening code of .NET and Richard D. proposed to derive this class from
System.Diagnostics.TraceListener, very good idea indeed as it simplified the calls too, thanks Richard. You just need to include the
System.Diagnostics namespace and call the
DebugConsole.Instance.Init() method. The first parameter tells if you want to set the debug listener (
true) or the trace listener (
false); the 2nd parameter concerns the carriage return for
WriteLine(), if it is set to
true, the message sent to
WriteLine will use a new line instead of being added to the current buffer. This happens when you use the
Write() are the only functions with the
static void Main()
Debug.WriteLine("Output will only appear in Debug Mode");
Trace.WriteLine("Output will appear in both Debug
and Release mode" + f.ToString());
This will immediately write the strings to the console and send them to the listeners (strings that you can visualize with an external tool like DebugView). The console is resizable now and I added an 'always on top' option.
The presence of the singleton also means you don't have to declare the object anywhere, these steps are automatically done by the class when you call
Init(). The window is placed at the top-left corner of the screen. You can change the color of the
ListView in the designer.
The timestamp uses the
DateTime class of .NET, you can use this class to add a 'date' button, milliseconds...Feel free to improve it