调试VSIX项目时如何添加控制台

问题描述 投票:0回答:1

如何将控制台添加到 VSIX 项目中,以便可以在任何扩展位置输出到它,例如使用

cout << "Hello"
logger.log("Hello")

c# visual-studio vsix
1个回答
0
投票

我发现的唯一方法是在 AddAnyFile 扩展

中实现的方法

它应该使用输出窗格。在上述项目中,它是通过以下方式实现的:

using Microsoft;
using Microsoft.VisualStudio.Shell.Interop;

internal static class Logger
{
    private static string name;
    private static IVsOutputWindowPane pane;
    private static IVsOutputWindow output;

    public static void Initialize(IServiceProvider provider, string name)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        output = (IVsOutputWindow)provider.GetService(typeof(SVsOutputWindow));
        Assumes.Present(output);
        Logger.name = name;
    }

    public static void Log(object message)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        try
        {
            if (EnsurePane())
            {
                pane.OutputString(DateTime.Now.ToString() + ": " + message + Environment.NewLine);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex);
        }
    }

    private static bool EnsurePane()
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        if (pane == null)
        {
            Guid guid = Guid.NewGuid();
            output.CreatePane(ref guid, name, 1, 1);
            output.GetPane(ref guid, out pane);
        }
        return pane != null;
    }
}

这个方法当然不方便,但总比没有好。

© www.soinside.com 2019 - 2024. All rights reserved.