2009-07-07 54 views
27

我正在编写一个自定义工具,并且我目前正在按照我想要的功能进行操作。如果出现问题,我希望能够写入Visual Studio。 (格式不正确的代码或其他)。如何在我的自定义工具中写入Visual Studio输出窗口?

是否有这方面的任何标准?现在我基本上可以强制该工具失败,并且Visual Studio会提示它已经这样做了。我希望Output窗口中有一个类别以及我想发送的任何结果消息。我也可以在错误列表窗口中使用更具描述性的任务/警告。

+0

为什么不写到标准输出为你工作? – avakar 2009-07-07 19:43:18

+0

向Console.Write写入消息不会在输出窗口中给我任何东西。 – 2009-07-07 20:11:23

回答

44

输出窗口

要写入“常规”输出窗口在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 

但是,如果你想写一个自定义窗口,这是你需要做的:在IVsOutputWindowIVsOutputWindowPane

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 

详细信息可以在MSDN上找到。

错误列表

对于将项目添加到错误列表,所述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); 
     ... 
    } 
    ... 
} 

GeneratorError()的详细信息可以在MSDN上找到。

4

如果你想要的东西出现在输出窗口,它必须来自标准输出。为此,您的应用需要作为“控制台”应用进行链接。在项目的属性页中设置/ SUBSYSTEM:CONSOLE标志,在链接器/系统下将SubSystem属性设置为CONSOLE。

一旦你在窗口中的输出,如果包括文本“错误:”它会出现一个错误,或者如果你设置“警告:”这将显示为警告。如果您的错误文本以路径/文件名开头,然后是括号中的行号,则IDE会将其识别为“可点击”错误,并自动导航到错误行。

+0

我在C#中这样做,项目属性中没有/子系统的东西,我需要做一个控制台应用程序来做到这一点?它现在作为DLL工作,但显然我没有得到输出。 – 2009-07-08 14:55:02

+0

这是一个DLL?它是如何被调用的?通常情况下,输出窗口捕获MSBuild构建的工具链中执行的工具,但据我所知,这些工具都是独立的可执行文件,MSBuild只是捕获所有stdout以便在窗口中显示。 刚才我发现您的自定义工具可能与建筑物无关。 “输出”窗口保留用于构建过程。如果你正在寻找运行时信息,你应该使用调试窗口(使用OutputDebugString()或Debug.Print()或其他)。 – 2009-07-08 16:51:00

-2

使用System.Diagnostics.Debugger.Message

7

有使用Marshal.GetActiveObject抓住当前DTE2实例的另一种方式。

首先参考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; 
    } 
} 
相关问题