我正在尝试将 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/