写入 Visual Studio 输出 window,万无一失的方法

Writing to Visual Studio output window, the sure-fire way

与“Writing to output window of Visual Studio?”中的要求完全相同,但解决了剩余问题 --

然而,事实并非如此,因为:

在正常 运行:

期间,我可以在 Visual Studio 输出 window 中看到一些信息打印
[1/10/2018 11:56:25 AM Informational] ------ Run test started ------
[1/10/2018 11:56:26 AM Informational] NUnit Adapter 3.9.0.0: Test execution started
[1/10/2018 11:56:26 AM Informational] Running selected tests in  ...\Demo.dll
[1/10/2018 11:56:26 AM Informational] NUnit3TestExecutor converted 14 of 14 NUnit test cases
[1/10/2018 11:56:45 AM Informational] NUnit Adapter 3.9.0.0: Test execution complete
[1/10/2018 11:56:45 AM Informational] ========== Run test finished: 1 run (0:00:19.3066647) ==========

对于 运行 在调试模式下,NUnit 调试输出将如下所示:

[1/10/2018 2:56:55 PM Informational] ------ Run test started ------
[1/10/2018 2:56:56 PM Informational] NUnit Adapter 3.9.0.0: Test execution started
[1/10/2018 2:56:56 PM Informational] Debugging selected tests in ...\Demo.dll
[1/10/2018 2:56:57 PM Informational] NUnit3TestExecutor converted 14 of 14 NUnit test cases
[1/10/2018 3:03:38 PM Informational] NUnit Adapter 3.9.0.0: Test execution complete
[1/10/2018 3:03:38 PM Informational] ========== Run test finished: 1 run (0:06:43.6161412) ==========

即,无论正常运行还是在调试模式下,NUnit适配器都能够写入Visual Studio输出window,区别仅在于文本“运行 选定的测试”或“调试选定的测试

所以,这里是调试输出案例的总结正常运行非调试模式

  • 对于 C# 控制台,Debug.WriteLineTrace.WriteLine 都可以正常工作。我已经创建并验证了这个:https://pastebin.com/Du6ZbDV3
  • 但是对于没有控制台的 Class/Form 等,Debug.WriteLineTrace.WriteLine 都不起作用。示例:https://pastebin.com/b7P0bYEa。 “它非常简单,但仍然一无所获 – previous-developer Feb 27 '12”。
  • 在 C# Class 库的 NUnit 测试中,Debug.WriteLineTrace.WriteLine 都不起作用。我已经 运行 我的好几次了,可以证实这一点。我不会在这里发布我的测试代码,因为它很长并且需要很多库才能工作,而且,它是由与 https://pastebin.com/b7P0bYEa 相同的故障引起的。

所以总而言之,如何从 class lib 或 win 表单写入 Visual Studio 输出 window?

要同时使用Debug.WriteLineTrace.WriteLine,我们需要添加using System.Diagnostics,它只在调试时有效(F5)。即使它是一个 C# 控制台项目 我们仍然无法在不调试启动 (Ctrl + F5) 时获得 Debug.WriteLineTrace.WriteLine 输出,要查看输出我们必须 运行 它在 调试 (F5)

对于 Class 库项目,它 只能构建或编译 ,我不认为我们可以启动或 运行 因为它不是一个控制台项目,除非它在控制台项目中被调用。

更新:

为了调试 class 库项目并显示其输出,我在 VS 的解决方案中添加了一个单元测试。 我的 class 图书馆代码:

    namespace ClassLibrary1
{
   public class hello
    {
        public static void foo()
        {
            Console.WriteLine("Hi, this is a console writeline");
            Debug.WriteLine("Hi, this is a Debug writeline");
        }
    }
}

在单元测试项目中,我将 ClassLibrary1 添加到参考

我的联合测试项目代码很简单:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;


namespace UnitTestProject3
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            hello.foo();

        }
    }
}

然后在 运行 测试方法之后我可以得到输出: