VSTO Visual Studio 2019 中未显示控制台输出

Console Outputs not showing in VSTO Visual Studio 2019

我正在编写有关如何为 Outlook (2016) 编写加载项的教程。我使用 Visual Studio 2019 作为 IDE 和 C# 作为语言。当我 运行 以下代码时,Outlook 打开,没有其他任何反应。我只是想弄清楚代码,所以我在里面放了一些输出。但是,它们不工作,正如我所知,"Console.WriteLine" 在 VSTO 中不起作用。所以我一开始就使用 "Debug.WriteLine" ,只是为了确保这个方法得到执行。

但在输出中找不到。我在调试应用程序时也没有找到。

我希望你知道为什么会这样,并能指导我正确的方向。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Diagnostics;

namespace OutlookAddIn1
{
public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Debug.WriteLine("HELLO WORLD");
        // Get the Application object
        Outlook.Application application = this.Application;

        // Get the Inspector object
        Outlook.Inspectors inspectors = application.Inspectors;

        // Get the active Inspector object
        Outlook.Inspector activeInspector = application.ActiveInspector();
        if (activeInspector != null)
        {
            // Get the title of the active item when the Outlook start.
           // MessageBox.Show("Active inspector: " + activeInspector.Caption);
            Console.WriteLine("Active Inspector: " + activeInspector.Caption);
        }

        // Get the Explorer objects
        Outlook.Explorers explorers = application.Explorers;

        // Get the active Explorer object
        Outlook.Explorer activeExplorer = application.ActiveExplorer();
        if (activeExplorer != null)
        {
            // Get the title of the active folder when the Outlook start.
          //  MessageBox.Show("Active explorer: " + activeExplorer.Caption);
            Console.WriteLine("Active explorer: " + activeInspector.Caption);
        }
        Console.ReadKey();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {

    }

    #region Von VSTO generierter Code

    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}
}

如果显示控制台 window,Console.WriteLine 方法就有意义了。如果没有,您可以使用 Debug.WriteLine 方法将有关调试的信息写入 Listeners 集合中的跟踪侦听器。默认情况下,输出写入 DefaultTraceListener 的实例。我建议在代码中设置断点以确保它完全是 运行。

如果您没有从加载项中获得任何输出,Microsoft Office 应用程序可以禁用行为异常的 VSTO 加载项。如果应用程序在您尝试调试时未加载您的 VSTO 外接程序,则该应用程序可能已硬禁用或软禁用您的 VSTO 外接程序。

当 VSTO 加载项导致应用程序意外关闭时,可能会发生硬禁用。如果您在 VSTO 外接程序中的 Startup 事件处理程序正在执行时停止调试器,它也可能发生在您的开发计算机上。

当 VSTO 外接程序产生不会导致应用程序意外关闭的错误时,可能会发生软禁用。例如,如果应用程序在执行启动事件处理程序时引发未处理的异常,则它可能会软禁用 VSTO 加载项。

当您重新启用软禁用的 VSTO 外接程序时,应用程序会立即尝试加载 VSTO 外接程序。如果最初导致应用程序软禁用 VSTO 插件的问题没有得到解决,应用程序将再次软禁用 VSTO 插件。

How to: Re-enable a VSTO Add-in that has been disabled 文章中阅读更多相关信息。

我们开发了一个 Word 加载项,允许用户在 Microsoft Word 中编辑 Chemistry。在这里,我们有很多 Debug.WriteLine 语句。

当运行在Visual Studio之外时IDE我们就得用极品了 System Internals Debug Viewer 查看调试输出

当 运行 在 Visual Studio IDE 中时,我们必须将启动项目设置为正在开发的 VSTO,并将调试器设置为在我们的案例 MS Word 中启动外部程序,如图所示以下 您显然应该将其设置为您安装的 Outlook 版本