Click here to Skip to main content
6,596,602 members and growing! (21,476 online)
Email Password   helpLost your password?
Web Development » Trace and Logs » Debug and Tracing     Intermediate

Debug Console Window

By dake / calodox

A console window class useful for debugging, reporting events during runtime and saving logs on disk.
C#, Windows, .NET 1.0, Dev
Posted:31 Aug 2002
Updated:1 Sep 2002
Views:111,912
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 5.67 Rating: 4.71 out of 5

1

2

3
2 votes, 14.3%
4
12 votes, 85.7%
5

Introduction

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() function. WriteLine() and Write() are the only functions with the override keyword.

using System.Diagnostics;

[STAThread]
static void Main() 
{
    #if (DEBUG)
    // debug mode


        DebugConsole.Instance.Init(true,true);
    #else
    // release mode

       DebugConsole.Instance.Init(false,true);
    #endif
    
    Application.Run(new Form1());
}

void MyFunction()
{
   float f=3.1415f;
   Debug.WriteLine("Output will only appear in Debug Mode");
   Trace.WriteLine("Output will appear in both Debug 
                   and Release mode" +   f.ToString());

   Debug.Write("1");
   Debug.Write("2");
}

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 :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dake / calodox


Member
Student at the EPFL (Lausanne/Switzerland) in informatics. I have been coding since 1996 and I am member of the Swiss demogroup Calodox.
Occupation: Web Developer
Location: France France

Other popular Trace and Logs articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralMake it thread dafe by add/change the following PinmemberhenryYYY13:04 10 Oct '07  
NewsDo it Threadsafe with BeginInvoke.... PinmemberTHaala11:30 17 Apr '07  
GeneralNext Gen VS 2005 Debug Console. Pinmemberriscy8:41 11 Aug '06  
QuestionRe: Next Gen VS 2005 Debug Console. Pinmemberriscy0:33 13 Aug '06  
AnswerRe: Next Gen VS 2005 Debug Console. PinmemberWolfgang G. Schmidt21:06 5 Feb '07  
GeneralRe: Next Gen VS 2005 Debug Console. Pinmemberriscy21:34 5 Feb '07  
QuestionRe: Next Gen VS 2005 Debug Console. PinmemberMarcus Deecke14:35 5 Jul '07  
GeneralLatest Build Pinmembertwesterd10:38 14 Apr '06  
GeneralThanks! Pinmemberhannahb8:38 4 Jul '05  
GeneralThreading problems Pinmemberstevz33:29 28 Nov '03  
GeneralVery helpful PinmemberMark Focas13:11 26 Nov '03  
GeneralSingleton & Namespace query PinmemberPaul Evans4:23 14 May '03  
GeneralBIG MSG - My Changes to Your code PinmemberPaul Evans6:26 14 May '03  
GeneralDebugConsole.Write PinmemberRichard_D0:11 2 Sep '02  
GeneralRe: DebugConsole.Write Pinmemberdake / calodox11:58 2 Sep '02  
GeneralRe: DebugConsole.Write PinmemberRichard_D0:15 3 Sep '02  
GeneralOutputDebugString PinmemberTodd Smith6:15 1 Sep '02  
GeneralRe: OutputDebugString PinmemberJoseph Dempsey7:27 1 Sep '02  
GeneralRe: OutputDebugString Pinmemberdake / calodox9:30 1 Sep '02  
GeneralRe: OutputDebugString Pinmemberszurgot13:41 2 Sep '02  
GeneralRe: OutputDebugString Pinmemberdake / calodox11:04 3 Sep '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Sep 2002
Editor: Smitha Vijayan
Copyright 2002 by dake / calodox
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project