Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I output Trace messages to a separate window in Visual C# 2008 Express?

Console, Debug, and Trace messages all seem to be displayed in the "Output" window, which is fine in most cases, but I'd like to have my Trace messages displayed separately (so they're not interleaved with everything else). Is there any way to do this without writing to a file/log?

like image 536
Mark Carpenter Avatar asked Mar 24 '10 12:03

Mark Carpenter


People also ask

How do I dock the Output window in Visual Studio?

To dock the Output window: Drag the title bar of the Output window to the left, right, top, or bottom edge of the InstallShield interface. To undock a tab on the Output window: Drag the tab to the new location.

How do I view traces in Visual Studio?

Using the Call Stack Window To open the Call Stack window in Visual Studio, from the Debug menu, choose Windows>Call Stack. To set the local context to a particular row in the stack trace display, select and hold (or double click) the first column of the row.

How do I trace code in Visual Studio?

Begin code stepping by selecting F10 or F11. Doing so allows you to quickly find the entry point of your app. You can then continue to press step commands to navigate through the code. Run to a specific location or function, for example, by setting a breakpoint and starting your app.


2 Answers

Sure, by default tracing writes to the Output window using the DefaultTraceListener, which is an implementation of the TraceListener class.

To write to a seperate output you'll need to either use one of the pre-supplied alternative TraceListeners or implement your own (handling, at a minimum, Write() and WriteLine(); then register your listener in your configuration file.

like image 177
STW Avatar answered Oct 23 '22 06:10

STW


There is only one option available in VS to get output in a different window: Tools + Options, Debugger, General, "Redirect all Output Window text to the Immediate Window". That however is not likely to do what you want it to do.

There are no good options available to get output into a VS window. The only mechanism is the Windows OutputDebugString() API function which lets the debugger see messages. That's already being used by the DefaultTraceListener. The Visual Studio hosting process supports redirecting Console.Write/Line() output to the Output window. The mechanism by which it works is unclear to me, other than that the hosting process is a custom hosted version of the CLR. Not something you'd want tackle, assuming it is even possible to replace it.

By far the most practical approach is to simply create your own window to display trace output, using your own trace listener. Easy enough to do with a Windows Forms form class that contains a multiline TextBox. How practical that is depends on the nature of your main EXE. Or trace to a file and use a file viewer that's smart enough to see updates to the file. I use Far.

Oh, and there's SysInternals' DebugView utility. It snoops on OutputDebugString() text.

like image 20
Hans Passant Avatar answered Oct 23 '22 06:10

Hans Passant