Is there a way of capturing debug messages in C# and then outputting them to a separate window?

GabeTHEGeek picture GabeTHEGeek · Jul 26, 2012 · Viewed 7.4k times · Source

I have spent 3 days looking this up and I cannot find a solid answer. I want to capture debug messages and than output them to a list log. I am trying to do this in C#. Would love some help from the community to point me in the right direction.

Answer

Cody Gray picture Cody Gray · Jul 26, 2012

Well, sure that's possible. Debug output is not limited to being displayed in the Output Window.

All you have to do is write a custom listener and add it to the Debug.Listeners collection. You can find the full documentation about adding trace listeners here on MSDN.

The only thing to watch out for is that the listeners are shared for debug and trace output, so if you add one, you'll receive both types of messages.

If that sounds like too much work and you just need a quick-and-dirty solution, you can download the free DebugView utility from Sysinternals. This neat little tool is a separate application that you run, and it listens to all debugging output from all of the programs installed on the machine. If you use this, you won't even have to change a single line of code in your application—all of the output send to Debug.Write will show up in the DebugView window.

Alternatively, if you're looking for something long-term that you could perhaps even ship with your application, I would encourage you to investigate adding a logging feature. There are lots of good open source libraries that provide this functionality, and it can be invaluable out in the field when your app is deployed to systems with unknown configurations.