I want to be able to see a time stamp in the beginning of every trace in the debug window in Visual studio.
[Time stamp here] The thread 'Win32 Thread' (0xcd0) has exited with code 0 (0x0). [Time stamp here] => CLR ProvideAssembly: AppDomainId: 1, Ref: 'msvcm90d...
Example of this is the sysinternals application - DebugView. The problem is that I can't have Visual Studio debugging, and listening with DebugView at the same time, and I am not comfortable with adding the time stamp manually to my tracers.
Since the output window text is read-only once written, there's not an easy way to do exactly what you want to do. However, it's easy to do something similar: append a timestamp line after new text is written to the output window. This will make the output window a lot more messy, but you'll get your timings.
Here's how this would work: First, create a Visual Studio Add-in or Macro which hooks the PaneUpdated event of the Outlook Window's active pane. (See this thread for how to do this with a Macro approach). Make sure to check, in the event handler, that pane.Name == "Debug"
and ignore other panes. Second, when you detect new text in the debug output pane, append a timestamp line, like this:
public void AddTimestamp(DTE2 dte)
{
// Retrieve and show the Output window.
OutputWindow outWin = dte.ToolWindows.OutputWindow;
pane = outWin.OutputWindowPanes.Item("Debug");
}
catch
{
pane = outWin.OutputWindowPanes.Add("Debug");
}
pane.OutputString("[timestamp: " + DateTime.Now.ToString() + "]\n");
}
It's also possible to pre-pend a timestamp to each line, but it's a lot harder. You can't change text already in the Output window (it's read-only), but you can clear the window and you can add text. So you could use the same event-handler approach above to detect text changes, but instead of appending you could copy the current text, prepend timestamps to any lines which don't have timestamps already, clear the window, and re-add the now-with-timestamps text. The problem with this is performance once your output window gets large. So you'd probably have to implement a kind of "lazy stamping" which does the clear and insert in the background, in order to avoid killing your IDE when (as is common) 100's of lines of debug output get emitted in a short time. Also, when you clear and re-add, if you're currently selecting text in the output window, your selection is lost.
Personally, I'd just do the easy thing and append timestamp lines rather than the harder pre-pend approach. Since stuff at the end of the line is hard to see without scrolling, I'd probably ensure there was a newline before the timestamp, so the user would see each batch of one or more output lines followed by one timestamp line.
It's possible there may be a way to hook the output window before text is displayed, but I couldn't find any such extensibility point in the VS Extensibility APIs.
One more idea: you could always roll your own tool window, e.g. "Ivan's Debug Output" which listens to events coming from the real output window, and echoes new lines (with timestamps) to your own tool window. This is probably the hardest option, but should do exactly what you want.