logging - Visual Studio 2005 中的简单宏输出/控制台日志记录?

标签 logging visual-studio-2005 console macros

我正在尝试将 Visual Studio 中的一个宏的输出记录到“输出”窗口。我认为 Debug.Print 会像在 Visual Basic.NET 和 VBA 中一样完成此任务,但它并没有这样做。

我找到了this ,并尝试过this ,它并不简单,在 Visual Studio 2005 中也不起作用(见下文):

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

Private Sub WriteOutput( _
ByVal s As String)

    Dim buffer As String

    buffer = buffer & Date.Now.ToLongTimeString()
    buffer = buffer & " "
    buffer = buffer & s
    buffer = buffer & vbCrLf

    Dim output As String = buffer.ToString()

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

请参阅下面的输出错误:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IndexOutOfRangeException' occurred in VBAssembly
The thread 0x23e4 has exited with code 0 (0x0).
The thread 0x1118 has exited with code 0 (0x0).
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

有什么简单的方法可以做到这一点吗?当我只想查看一些简单的控制台输出时,我厌倦了不得不弹出一个消息框(主要是因为当您运行脚本时,您必须单击“确定”无数次)。

最佳答案

这是我在宏中使用的 GetOutputWindow,您向它传递一个名称 - 我相信它是 Visual Studio 附带的标准宏的一部分,因此您应该能够像 Utilities 一样引用它。获取输出窗口 Pane

Private Sub WriteOutput(ByVal s As String)

    Dim buffer As String

    buffer = buffer & Date.Now.ToLongTimeString()
    buffer = buffer & " "
    buffer = buffer & s
    buffer = buffer & vbCrLf

    Dim output As String = buffer.ToString()

    Dim outputPane As OutputWindowPane = GetOutputWindowPane("Macros")
    outputPane.OutputString(output)
End Sub

Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
    Dim window As Window
    Dim outputWindow As OutputWindow
    Dim outputWindowPane As OutputWindowPane

    window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    If show Then window.Visible = True
    outputWindow = window.Object
    Try
        outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
    Catch e As System.Exception
        outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
    End Try
    outputWindowPane.Activate()
    Return outputWindowPane
End Function

关于logging - Visual Studio 2005 中的简单宏输出/控制台日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2007850/

相关文章:

Emacs Ctrl 修饰符在控制台中不起作用

java - 对于使用现有库的 Web 应用程序来说,最安全的日志记录框架是什么?

javascript - 向 Winston 传输添加不同的格式化程序

java - 使用 log4j 进行日志记录的 JUnit 默认配置是什么以及如何使用 eclipse 更改它?

java - 如何防止 java.util.logging.Logger 在标准输出上显示消息?

visual-studio - Outlook 加载项 VSTO : Ribbon Type property is not shown

VS2005 中的 C# : Can a device project target both full framework and CF?

c++ - 忽略数据之前的 scanf 中的回车...以使用 conio.h 保持基于控制台的图形布局

sharepoint - 为什么在我的项目中复制了 Microsoft.SharePoint.Search.dll?

c# - 防止表单在服务/控制台中显示