How to add console when debugging VSIX project

42 views Asked by At

How to add console to VSIX project in the way you can output to it at any place of extension, for example using cout << "Hello" or logger.log("Hello")?

1

There are 1 answers

0
qloq On

The only way I've found yet is one implemented in AddAnyFile extension

It is supposed to use output pane. In the above mentioned project it is implemented in the following way:

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;
    }
}

This method is inconvient of course, but is better than nothing.