Using DebugView and C#

DebugView is a very nice tool from Microsoft (by SysInternals), which allows you to see debug output from the kernel or Win32, local and remote!

Providing debug messages trough this system has been available from Windows 95, so you can trust having debug capabilities everywhere. I strongly belief this to be a very powerful way to provide information about your application, without having to write to logfiles, or attaching a debugger. Debugging ASP.NET applications should also be easier when incorporating this.

But how do you make C# use this? You use the System.Diagnostics namespace!

I have created a small demonstration program which writes two messages and a third one conditionally. Putting a prefix in front of the messages will help you filtering them out in DebugView afterwards, since other applications might be writing to it as well.

[csharp]using System; using System.Diagnostics;

namespace CumpsD.Demo.DebugView { class Program { static void Main(string[] args) { Console.WriteLine("Demonstrating System.Diagnostics"); Console.WriteLine("--------------------------------" + Environment.NewLine);

  Console.WriteLine("Turn on DebugView to look at results.");
  Console.WriteLine("http://www.microsoft.com/technet/sysinternals/utilities/debugview.mspx");
  Console.WriteLine();
  Console.Write("Press any key to continue . . .");
  Console.ReadKey(true);
  Console.WriteLine(Environment.NewLine);

  Debug.Write("DemoCategory: This is written trough System.Diagnostics.Debug.Write.");
  Console.WriteLine("This is written trough System.Diagnostics.Debug.Write.");

  Trace.Write("[DemoCategory: This is written trough System.Diagnostics.Trace.Write.");
  Console.WriteLine("This is written trough System.Diagnostics.Trace.Write.");

  bool inDebug = (new Random().Next(0, 2) == 1);
  Debug.WriteIf(inDebug, "This is written because inDebug is true.");

  if (inDebug)
  {
    Console.WriteLine("This is written because inDebug is true.");
  }
  Console.WriteLine(Environment.NewLine + "Look in DebugView to see the messages.");
}

} } [/csharp]

When you run this program, you will get the following output in DebugView:

DebugView Output

It’s also possible to connect to a remote computer, and intercept these debug messages remotely.

DebugView Remote

This tool is completely free, which is really amazing for such a powerful and useful tool.