我正在编写一个自定义工具,并且我目前正在按照我想要的功能进行操作。如果出现问题,我希望能够写入Visual Studio。 (格式不正确的代码或其他)。如何在我的自定义工具中写入Visual Studio输出窗口?
是否有这方面的任何标准?现在我基本上可以强制该工具失败,并且Visual Studio会提示它已经这样做了。我希望Output窗口中有一个类别以及我想发送的任何结果消息。我也可以在错误列表窗口中使用更具描述性的任务/警告。
我正在编写一个自定义工具,并且我目前正在按照我想要的功能进行操作。如果出现问题,我希望能够写入Visual Studio。 (格式不正确的代码或其他)。如何在我的自定义工具中写入Visual Studio输出窗口?
是否有这方面的任何标准?现在我基本上可以强制该工具失败,并且Visual Studio会提示它已经这样做了。我希望Output窗口中有一个类别以及我想发送的任何结果消息。我也可以在错误列表窗口中使用更具描述性的任务/警告。
要写入“常规”输出窗口在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
但是,如果你想写一个自定义窗口,这是你需要做的:在IVsOutputWindow和IVsOutputWindowPane
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上找到。
您可以使用调试和/或跟踪类。这里有一些信息: http://msdn.microsoft.com/en-us/library/bs4c1wda(VS.71).aspx
祝你好运。
在询问之前,我试过trace.write(message,category),它没有输出。 – 2009-07-07 20:07:34
如果你想要的东西出现在输出窗口,它必须来自标准输出。为此,您的应用需要作为“控制台”应用进行链接。在项目的属性页中设置/ SUBSYSTEM:CONSOLE标志,在链接器/系统下将SubSystem属性设置为CONSOLE。
一旦你在窗口中的输出,如果包括文本“错误:”它会出现一个错误,或者如果你设置“警告:”这将显示为警告。如果您的错误文本以路径/文件名开头,然后是括号中的行号,则IDE会将其识别为“可点击”错误,并自动导航到错误行。
我在C#中这样做,项目属性中没有/子系统的东西,我需要做一个控制台应用程序来做到这一点?它现在作为DLL工作,但显然我没有得到输出。 – 2009-07-08 14:55:02
这是一个DLL?它是如何被调用的?通常情况下,输出窗口捕获MSBuild构建的工具链中执行的工具,但据我所知,这些工具都是独立的可执行文件,MSBuild只是捕获所有stdout以便在窗口中显示。 刚才我发现您的自定义工具可能与建筑物无关。 “输出”窗口保留用于构建过程。如果你正在寻找运行时信息,你应该使用调试窗口(使用OutputDebugString()或Debug.Print()或其他)。 – 2009-07-08 16:51:00
使用System.Diagnostics.Debugger.Message
有使用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;
}
}
为什么不写到标准输出为你工作? – avakar 2009-07-07 19:43:18
向Console.Write写入消息不会在输出窗口中给我任何东西。 – 2009-07-07 20:11:23