在 VS 扩展中如何在输出 window 中显示一些字符串(包含路径和行号)

In a VS extension how to dispaly the some string (which contains the path and line number) in output window

我正在使用 C# 在 VS 2017 中开发一个扩展。我想 运行 单击按钮时的静态代码分析器,其输出将显示在 [= 的输出 window 中17=] with error/warning/info with full path (as that in Building Case) and do do do clicking that It should navigate me to the line where error is present.

我可以将普通字符串添加到输出 window。但我想要一种格式,这样我就可以点击它,点击它会导航到该行。

private void Execute(object sender, EventArgs e)
        {
            Output("Created Pane");
        }
        public static void Output(string msg)
        {
            var pane = GetWindow();
            // Output the message
            pane.OutputString(msg + Environment.NewLine);
        }
        private static IVsOutputWindowPane GetWindow()
        {
            var outputWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;

            // Ensure that the desired pane is visible
            var paneGuid = Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid;
            IVsOutputWindowPane pane;

            outputWindow.CreatePane(paneGuid, "General", 1, 1);
            outputWindow.GetPane(paneGuid, out pane);
            return pane;
        }

您应该像这样格式化消息:

$"{e.file}({e.line + 1},{e.column}): error {e.code}: {e.message}"

方法 OutputTaskItemString 是解决我的 problem.This 的方法,也会添加到错误 window 中。您可以根据需要更改 VSTASKPRIORITY 和 VSTASKCATEGORY。

pane.OutputTaskItemString("D:\VSIXDemo\Command1.cs(98): error C2143 : syntax error: missing ';' before '}' \n", Microsoft.VisualStudio.Shell.Interop.VSTASKPRIORITY.TP_HIGH, Microsoft.VisualStudio.Shell.Interop.VSTASKCATEGORY.CAT_BUILDCOMPILE, "Error C2143", (int)Microsoft.VisualStudio.Shell.Interop._vstaskbitmap.BMP_COMPILE, "D:\VSIXDemo\Command1.cs", 98, "Error C2143: syntax error : missing ';' before '}'");

//98 是行号,这会将 ypou 导航到第 99 行,因为它是基于 0 的索引