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

StackOverflow https://stackoverflow.com//questions/11671413

Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

As far as logging goes, Log4Net is solid and easy to use. You can add it to your project via NuGet (if you don't know what NuGet is, check it out: it's really nice). It allows you to log more or less wherever you want (console, file, mail, db ... )

Website contains good documentation and examples.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top