9

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.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Ivan Zlatanov
  • 5,146
  • 3
  • 29
  • 45

5 Answers5

7

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.

Community
  • 1
  • 1
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
  • @Justin I was looking at that extensibility also. End of the line is not very nice, as lines have different lengths and its very hard to read. The repainting of the window is a killer as you say... Thanks for your answer, but I am really looking into finding how to do this, even if its hackish. – Ivan Zlatanov Nov 09 '09 at 22:27
  • Yep, instead of end-of-line, if you go with an append-only solution, I'd suggest planting the timestamp on a new line-- this doubles the line count of the output window, but is better (IMHO) than having timestamps buried at end-of-line. Plus, it may not even be possible to plant the text at end-of-line, if the newlines are already emitted into the document before you get control. – Justin Grant Nov 09 '09 at 23:18
  • 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. – Justin Grant Nov 09 '09 at 23:20
  • @Justin Good idea. I'll try to see what comes out of it. – Ivan Zlatanov Nov 10 '09 at 08:30
  • @Justin A quick note before I accept your answer. You are correct, the only possible way to handle this is to create another window, which to watch for changes in the Output pane. I did that and worked, though I hate the solution it's the only thing I can do at the moment. Thanks for the suggestions and the information. – Ivan Zlatanov Nov 15 '09 at 16:51
3

to add a new answer to an ANCIENT question, there's an feature in the productivity power tools 2013 extension and productivity power tools 2015 extension that add a timestamp margin to the output window, no code required.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • 2
    There is now a more lightweight [standalone](https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.TimeStampMargin) extension for VS2017 – user1007074 Aug 30 '17 at 15:15
  • The 2017 extension is a nice addition. Unfortunately it doesn't display timestamps when compiling, which is what I was hoping for. Compiling UWP code is painful on my setup. – Randall Deetz Apr 03 '19 at 21:20
2

I was looking for the same functionality. Colleague of mine came up with the $TICK macro in the message field, printing out the 'current' cpu ticks.

Take a look at these tips from Simon Chapman, too.

xtofl
  • 40,723
  • 12
  • 105
  • 192
1

In Visual Studio 2022 17.4 and higher you have this option

2022 17.4 Release Notes What's new

In the output windows there is a button with clock.

enter image description here



More info:

https://davecallan.com/timestamps-added-output-window-visual-studio/

AnGG
  • 679
  • 3
  • 9
0

I wanted this functionality too, so eventually I wrote an extension to do it (much like Justin Grant suggested in the accepted answer). After using it for a while, I decided that relative timestamps were even more useful to me when debugging. If folks wanted absolute timestamps, I'm sure I could add that functionality back in. Anyway, if you're interested you can check it out at niahtextfilter.com.

And to show the relative timestamps in action in a Visual Studio debug session: Niah Text Filter debug output

GrantTheAnt
  • 871
  • 8
  • 11