Chris Farmer
Chris Farmer

Reputation: 25415

How can I get a reference to an output stream that writes to the Visual Studio "output" window?

I have a third-party .NET library that enables output of some key diagnostic information to a TextWriter instance. I'd like to have this information dumped to the output window of Visual Studio 2008 while in debug mode and ignored if not in debug mode. Is there any way I can get a reference to an output stream that uses this output window so I can create a TextWriter that uses it?

To use the console for output, I can do...

foo.LogTo(Console.Out);

Using this interface, can I get log info sent to the output window?

Upvotes: 5

Views: 769

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273572

You can create your own TextWriter derived class, using the OutputDebugString API or use Debug.WriteLine() to forward the data.

Upvotes: 1

casperOne
casperOne

Reputation: 74550

Visual Studio has allowed for the writing of messages to the output window through the Debug class since .NET 1.1.

With that said, it's easy enough to create your own TextWriter implementation which would just forward calls to the appropriate methods on the Debug class. When the content is written to your TextWriter, you would call the Debug class method and it will appear in the output window.

Also, because you are making the calls to the Debug class, when you are not in debug mode, the output wont be output (because of the Conditional attribute on the methods of the Debug class).

Upvotes: 1

Related Questions