visual-studio 如何在我的自定义工具中写入 Visual Studio 输出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1094366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write to the Visual Studio Output Window in My Custom Tool?
提问by Jeff Martin
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).
我正在编写一个自定义工具,目前我让它在功能方面做我想做的事情。如果出现问题,我希望能够写入 Visual Studio。(格式不正确的代码或其他什么)。
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.
这有什么标准吗?现在我基本上可以强制该工具失败,Visual Studio 会发出警告,表明它已经这样做了。我想要输出窗口中的一个类别,其中包含我想要发送的任何结果消息。我还可以在错误列表窗口中接受更具描述性的任务/警告。
回答by Alex
Output Window
输出窗口
To write to the "General" output window in Visual Studio, you need to do the following:
要写入 Visual Studio 中的“常规”输出窗口,您需要执行以下操作:
IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;
Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane; // P.S. There's also the GUID_OutWindowDebugPane available.
IVsOutputWindowPane generalPane;
outWindow.GetPane( ref generalPaneGuid , out generalPane );
generalPane.OutputString( "Hello World!" );
generalPane.Activate(); // Brings this pane into view
If, however, you want to write to a custom window, this is what you need to do:
但是,如果您想写入自定义窗口,则需要执行以下操作:
IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;
// Use e.g. Tools -> Create GUID to make a stable, but unique GUID for your pane.
// Also, in a real project, this should probably be a static constant, and not a local variable
Guid customGuid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789");
string customTitle = "Custom Window Title";
outWindow.CreatePane( ref customGuid, customTitle, 1, 1 );
IVsOutputWindowPane customPane;
outWindow.GetPane( ref customGuid, out customPane);
customPane.OutputString( "Hello, Custom World!" );
customPane.Activate(); // Brings this pane into view
Details on IVsOutputWindowand IVsOutputWindowPanecan be found on MSDN.
可以在 MSDN 上找到有关IVsOutputWindow和IVsOutputWindowPane 的详细信息。
Error List
错误列表
For adding items to the error list, the IVsSingleFileGeneratorhas a method call void Generate(...)which has a parameter of the type IVsGeneratorProgress. This interface has a method void GeneratorError()which lets you report errors and warnings to the Visual Studio error list.
要将项目添加到错误列表,IVsSingleFileGenerator有一个方法调用void Generate(...),该方法调用具有类型为 的参数IVsGeneratorProgress。这个接口有一个方法void GeneratorError()可以让你向 Visual Studio 错误列表报告错误和警告。
public class MyCodeGenerator : IVsSingleFileGenerator
{
...
public void Generate( string inputFilePath, string inputFileContents, string defaultNamespace, out IntPtr outputFileContents, out int output, IVsGeneratorProgress generateProgress )
{
...
generateProgress.GeneratorError( false, 0, "An error occured", 2, 4);
...
}
...
}
The details of GeneratorError()can be found on MSDN.
GeneratorError()的详细信息可以在 MSDN 上找到。
回答by Ian Davis
There is another way using Marshal.GetActiveObjectto grab a running DTE2instance.
还有另一种方法可以Marshal.GetActiveObject用来抓取正在运行的DTE2实例。
First reference EnvDTE and envdte80. This currently works in VisualStudio 2012, I haven't tried the others yet.
先参考EnvDTE和envdte80。这目前在 VisualStudio 2012 中有效,我还没有尝试过其他的。
using System;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
internal class VsOutputLogger
{
private static Lazy<Action<string>> _Logger = new Lazy<Action<string>>( () => GetWindow().OutputString );
private static Action<string> Logger
{
get { return _Logger.Value; }
}
public static void SetLogger( Action<string> logger )
{
_Logger = new Lazy<Action<string>>( () => logger );
}
public static void Write( string format, params object[] args)
{
var message = string.Format( format, args );
Write( message );
}
public static void Write( string message )
{
Logger( message + Environment.NewLine );
}
private static OutputWindowPane GetWindow()
{
var dte = (DTE2) Marshal.GetActiveObject( "VisualStudio.DTE" );
return dte.ToolWindows.OutputWindow.ActivePane;
}
}
回答by John Deters
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.
如果您希望在“输出”窗口中显示任何内容,则它必须来自标准输出。为此,您的应用需要链接为“控制台”应用。在项目的属性页中设置 /SUBSYSTEM:CONSOLE 标志,在链接器/系统下将子系统属性设置为 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.
一旦您在窗口中显示输出,如果您包含文本“错误:”,它将显示为错误,或者如果您设置了“警告:”,它将显示为警告。如果您的错误文本以路径/文件名开头,后跟括号中的行号,IDE 会将其识别为“可点击”错误,并自动将您导航到错误行。
回答by Jon Onstott
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
您可以使用 Debug 和/或 Trace 类。这里有一些信息:http: //msdn.microsoft.com/en-us/library/bs4c1wda(VS.71).aspx
Best of luck.
祝你好运。
回答by David Egli
use System.Diagnostics.Debugger.Message
利用 System.Diagnostics.Debugger.Message

