Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal

Search

Search

Tracing and Logging from Visual Studio Macros

Note
This post originally appeared on my MSDN blog:

Since I no longer work for Microsoft, I have copied it here in case that blog ever goes away.

As I mentioned in a post last week, I often use macros in Visual Studio to automate development tasks.

Before sharing some of my most frequently used macros, however, I wanted to first introduce the method I use to trace events and log messages while running various macros.

Take a look at the following Output window from Visual Studio. Notice how there is an item in the Show output from dropdown list titled Macros (a.k.a. "the macro output pane").

Macro output pane
Figure 1: Macro output pane

Chances are that when you look at your Visual Studio environment, you won't see this item. So, why does it appear in my environment?

The macro output pane is actually created on-the-fly, as necessary, whenever I run one of my macros that implements tracing (i.e. writes output). This is done via the GetMacroOutputPane() function, as shown below.

    Private Function GetMacroOutputPane() As OutputWindowPane
        Dim ow As OutputWindow = _
            DTE.Windows.Item(Constants.vsWindowKindOutput).Object()

        Dim outputPane As OutputWindowPane

        Try
            outputPane = ow.OutputWindowPanes.Item("Macros")
        Catch ex As Exception
            outputPane = ow.OutputWindowPanes.Add("Macros")
        End Try

        Return outputPane
    End Function

Pretty simple, eh? If the macro output pane exists, then use it; otherwise add a new pane.

In order to simplify writing output messages -- as well as timestamp each message as it is written -- I use the WriteOutput() method

    Private Sub WriteOutput( _
        ByVal s As String)

        Dim buffer As StringBuilder = New StringBuilder

        buffer.Append(Date.Now.ToLongTimeString())
        buffer.Append(" ")
        buffer.Append(s)
        buffer.Append(vbCrLf)

        Dim output As String = buffer.ToString()

        Dim outputPane As OutputWindowPane = GetMacroOutputPane()
        outputPane.OutputString(output)
    End Sub

Comments

  1. # re: Tracing and Logging from Visual Studio Macros

    May 23, 2011 10:24 PM
    max

    You need to show your imports dude.

  2. # Visual Studio Macros for Unloading/Reloading Projects

    March 11, 2009 10:10 AM
    blogs.msdn.com

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Watermelon

Pear
Grapes
Watermelon
Strawberry
Apple
Cherries
 
Please add 4 and 4 and type the answer here: