如何在我的自定义工具中写入 Visual Studio 输出窗口?
我正在编写一个自定义工具,目前我已经让它完成我想要的功能。 如果出现问题,我希望能够写入 Visual Studio。 (代码格式不正确或其他)。
这有什么标准吗? 现在,我基本上可以强制该工具失败,并且 Visual Studio 会发出警告,表明它已经这样做了。 我希望在“输出”窗口中包含一个类别,其中包含我想要发送的任何结果消息。 我还可以在错误列表窗口中接受更具描述性的任务/警告。
I am writing a custom tool and I currently have it doing what I want as far as functionality. I would like to be able to write to Visual Studio if something goes wrong. (Incorrectly formatted code or whatever).
Are there any standards for this? Right now I basically can force the tool to fail and Visual Studio puts in a warning that it has done so. I'd like a category in the Output window with any resulting messages I want to send. I could also live with a more descriptive task/warning in the Error list window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
输出窗口
要写入 Visual Studio 中的“常规”输出窗口,您需要执行以下操作:
但是,如果您想要写入自定义窗口,则需要执行以下操作:
有关 IVsOutputWindow 和 IVsOutputWindowPane 可以在 MSDN 上找到。
错误列表
为了将项目添加到错误列表,
IVsSingleFileGenerator
有一个方法调用voidGenerate(...)
,该方法具有IVsGeneratorProgress
类型的参数代码>. 此接口有一个方法void GeneratorError()
,可让您向 Visual Studio 错误列表报告错误和警告。GeneratorError() 可以在 MSDN 上找到。
Output Window
To write to the "General" output window in Visual Studio, you need to do the following:
If, however, you want to write to a custom window, this is what you need to do:
Details on IVsOutputWindow and IVsOutputWindowPane can be found on MSDN.
Error List
For adding items to the error list, the
IVsSingleFileGenerator
has a method callvoid Generate(...)
which has a parameter of the typeIVsGeneratorProgress
. This interface has a methodvoid GeneratorError()
which lets you report errors and warnings to the Visual Studio error list.The details of GeneratorError() can be found on MSDN.
还有另一种方法使用
Marshal.GetActiveObject
来获取正在运行的DTE2
实例。首先参考EnvDTE和envdte80。 目前这适用于 VisualStudio 2012,我还没有尝试过其他的。
There is another way using
Marshal.GetActiveObject
to grab a runningDTE2
instance.First reference EnvDTE and envdte80. This currently works in VisualStudio 2012, I haven't tried the others yet.
如果您希望任何内容出现在“输出”窗口中,则它必须来自标准输出。 为此,您的应用程序需要作为“控制台”应用程序链接。 在项目的属性页中设置 /SUBSYSTEM:CONSOLE 标志,在 Linker/System 下将 SubSystem 属性设置为 CONSOLE。
在窗口中获得输出后,如果包含文本“错误:”,它将显示为错误,或者如果设置“警告:”,它将显示为警告。 如果错误文本以路径/文件名开头,后跟括号中的行号,IDE 会将其识别为“可单击”错误,并自动导航到故障行。
If you want anything to appear in the Output window, it has to come from stdout. To do this, your app needs to be linked as a "console" app. Set the /SUBSYSTEM:CONSOLE flag in the project's property page, under Linker/System set the SubSystem property to CONSOLE.
Once you have your output in the window, if you include the text "Error:" it will appear as an error, or if you set "Warning:" it will appear as a warning. If your error text begins with a path/filename, followed by a line number in parenthesis, the IDE will recognize it as a "clickable" error, and navigate you automatically to the faulting line.
Microsoft 示例项目中的以下帮助程序类对此进行了演示:
https://github.com/microsoft/VSSDK-Extensibility-Samples/blob/df10d37b863feeff6e8fcaa6f4d172f602a882c5/Reference_Services/C%23/Reference.Services/HelperFunctions.cs#L28
代码如下:
This is demonstrated in the following helper class from a Microsoft sample project:
https://github.com/microsoft/VSSDK-Extensibility-Samples/blob/df10d37b863feeff6e8fcaa6f4d172f602a882c5/Reference_Services/C%23/Reference.Services/HelperFunctions.cs#L28
The code is as follows:
您可以使用 Debug 和/或 Trace 类。 这里有一些信息:
http://msdn.microsoft.com/en-us /library/bs4c1wda(VS.71).aspx
祝你好运。
You can use the Debug and/or Trace classes. There is some information here:
http://msdn.microsoft.com/en-us/library/bs4c1wda(VS.71).aspx
Best of luck.
使用System.Diagnostics.Debugger.Message
use
System.Diagnostics.Debugger.Message