简体   繁体   English

如何从Visual Studio调试输出窗口中读取文本

[英]How to read in text from the visual studio debug output window

I've read several articles that tell you how to add text to the output window in visual studio from within an Add-On (specifically, a visual studio 2008 integration package, via the visual studio 2008 SDK 1.1), but no examples of how to read text from the output window. 我已经阅读了几篇文章,这些文章告诉您如何从附加组件(特别是Visual Studio 2008集成包,通过visual Studio 2008 SDK 1.1)中将文本添加到Visual Studio的输出窗口中,但是没有有关如何从输出窗口读取文本。 My goal is to parse text from the debug output window while debugging a certain application (TRACE output and possibly stdin/stdout). 我的目标是在调试某个应用程序(TRACE输出以及可能的stdin / stdout)时,从调试输出窗口中解析文本。 The IVsOutputWindowPane interface has no methods for reading in text from the output window. IVsOutputWindowPane接口没有用于从输出窗口读取文本的方法。 The documentation seems to imply that it is possible, but it doesn't provide an example: 该文档似乎暗示有可能,但没有提供示例:

http://msdn.microsoft.com/en-us/library/bb166236(VS.80).aspx http://msdn.microsoft.com/zh-cn/library/bb166236(VS.80).aspx

Quote: In addition, the OutputWindow and OutputWindowPane objects add some higher-level functionality to make it easier to enumerate the Output window panes and to retrieve text from the panes. Quote:另外,OutputWindow和OutputWindowPane对象添加了一些更高级别的功能,以使枚举Output窗口窗格以及从窗格中检索文本变得更加容易。

Preferably I'd like to be able to subscribe to an event that fires when a new line of text arrives, similar to a StreamReader's asynchronous reads. 最好是,我希望能够订阅一个事件,该事件在一行新文本到达时触发,类似于StreamReader的异步读取。

It is possible, it is just a long winding path to get to it: 可能这是一条漫长的曲折道路:

ServiceProvider -> IVsOutputWindow -> GetPane( debugwindow ) -> IVsUserData -> GetData( wpftextviewhost ) -> IWpfTextViewHost -> IWpfTextView -> TextBuffer -> Changed event. ServiceProvider-> IVsOutputWindow-> GetPane(debugwindow)-> IVsUserData-> GetData(wpftextviewhost)-> IWpfTextViewHost-> IWpfTextView-> TextBuffer->更改的事件。

Presuming you have a VS IServiceProvider from somewhere else (vsix extension/whatever, global service provider), and without any error checking, it looks like this: 假设您从其他地方(vsix扩展/无论是什么,全局服务提供者)那里都有一个VS IServiceProvider ,并且没有任何错误检查,它看起来像这样:

IVsOutputWindow outWindow = ServiceProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
Guid debugPaneGuid = VSConstants.GUID_OutWindowDebugPane;
IVsOutputWindowPane pane;
outWindow.GetPane(ref debugPaneGuid, out pane);
// from here up you'll find in lots of other stackoverflow answers, 

// the stuff from here down is interesting to this question
IVsUserData userData = (IVsUserData)pane;
object o;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out o);

IWpfTextViewHost viewHost = (IWpfTextViewHost)o;
IWpfTextView textView = viewHost.TextView;
textView.TextBuffer.Changed += YourTextChangedHandlerHere;

Your text changed handler will then get called every time the output window gets more data. 每当输出窗口获取更多数据时,将调用您的文本更改处理程序。 you won't necessarily get it line by line, but you'll probably more likely than not get big chunks you'll need to deal with on your own. 您不一定会逐行获得它,但您很有可能会获得不需要独自处理的大块。

It is highly likely that some of the above did not even exist in VS in 2010. But it exists now! 很有可能在2010年VS中甚至不存在上述某些内容。但是现在已经存在了!

The default behavior (when you don't set the listener explicitly) of VS is to display trace massages in the debugger output window, which you appreciate if you want a simple solution and do no other actions with the massages. VS的默认行为(当您未显式设置侦听器时)是在调试器输出窗口中显示跟踪消息,如果您想要一个简单的解决方案并且对消息不执行其他任何操作,则不胜感激。

Unfortunately this is not your case. 不幸的是,这不是您的情况。 So you have to define a trace listener to send (and store) your trace massages where you then will be able to read them. 因此,您必须定义一个跟踪侦听器,以发送(并存储)您的跟踪消息,然后才能在其中读取它们。 The trace listener could be a file (for example XML) or you can create a custom listener by deriving a class from the base class TraceListener if you don't want to bother yourself with an additional file. 跟踪侦听器可以是一个文件(例如XML),也可以通过从基类TraceListener派生一个类来创建自定义侦听器,如果您不想麻烦其他文件。

I don't know that what you ask is possible. 我不知道你问的是可能的。 But, you can register your add-in as a debugger for your application so that you get the output the trace messages. 但是,您可以将外接程序注册为应用程序的调试器,以便获得跟踪消息的输出。 These are typically routed to OutputDebugString, and can be captured as described in this article: http://www.drdobbs.com/showArticle.jhtml?articleID=184410719 . 这些通常路由到OutputDebugString,并且可以按照本文中的描述进行捕获: http : //www.drdobbs.com/showArticle.jhtml ?articleID=184410719。 It does not give you the normal output, only debug, but it does not depend on the technology of the debugged application. 它不提供正常的输出,仅提供调试,但不取决于调试的应用程序的技术。

The solution on this page selects the text in order to read it. 此页面上的解决方案选择文本以便阅读。 I'm hoping there's a better way. 我希望有更好的方法。 Automatically stop Visual C++ 2008 build at first compile error? 自动停止Visual C ++ 2008年生成第一次编译错误?

Private Sub OutputWindowEvents_OnPaneUpdated(ByVal pPane As OutputWindowPane) Handles OutputWindowEvents.PaneUpdated
    pPane.TextDocument.Selection.SelectAll()
    Dim Context As String = pPane.TextDocument.Selection.Text
    pPane.TextDocument.Selection.EndOfDocument()
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在调试输出窗口中删除不需要的消息 - Visual Studio 上的 Xamarin - How to remove unwanted messages in debug output window - Xamarin on visual studio Visual Studio中输出窗口没有调试选项 - No debug option for output window in Visual Studio Visual Studio调试窗口条输出 - Visual Studio debug window strips output 如何从 c# 将彩色文本写入 Visual Studio 输出窗口? - How do I write color text to the Visual Studio output window from c#? 如何在Visual Studio 2010中调试Window服务 - how to debug Window service in visual studio 2010 Visual Studio 2010输出窗口缺少调试信息 - Visual Studio 2010 output window missing debug info 如何在Visual Studio的R#Unit Test Session窗口中输出单元测试的彩色文本? - How do I output coloured text from by unit tests in the R# Unit Test Session window in Visual Studio? 如何将单独控制台 window 的 output 传递给 Visual Studio output Z05B8C74CBD96FBF2DE4C1A352702? - How to pass output of separate console window to Visual Studio output window? 可以在 Visual Studio 的 output window 中查看 OutputDebugString 中的 output 吗? - Can output from OutputDebugString be viewed in Visual Studio's output window? 如何在Visual Studio 2015输出窗口中阻止C#编译器输出? - How do I stop the C# compiler output from wrapping in the Visual Studio 2015 output window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM