写入 Visual Studio 输出窗口,万无一失的方式

xpt*_*xpt 2 c# debugging visual-studio visual-studio-debugging

与“写入 Visual Studio 的输出窗口? ”中所问的完全一样,但是解决了剩余的问题——

  • 我假设如果我在没有调试(ctrl-f5)的情况下开始,就没有办法写入输出,对吗?– previous_developer 2012 年 2 月 27 日

  • Debug.WriteLine() 仅在 Debug 中运行时才有效。这意味着使用 F5 而不是 CTRL-F5 运行它。– kirk.burleson 2013 年 7 月 15 日

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

  • 刚刚在这里运行了一个小应用程序,对我来说很好用。也许您的环境中存在小故障?– Bhargav Bhat 2012 年 2 月 27 日

  • 而且,

在正常运行期间,我可以在 Visual Studio 输出窗口看到一些信息打印:

[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) ==========
Run Code Online (Sandbox Code Playgroud)

为了在调试模式下运行,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) ==========
Run Code Online (Sandbox Code Playgroud)

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

所以,这里是在正常运行期间调试输出情况的总结,而不是在调试模式下

  • 对于C#控制台,都Debug.WriteLineTrace.WriteLine工作正常。我已经创建并验证了这个:https : //pastebin.com/Du6ZbDV3
  • 但是对于没有控制台的类/表单等,既不工作Debug.WriteLine也不Trace.WriteLine工作。示例:https : //pastebin.com/b7P0bYEa。“这很简单,但仍然什么都没有——前任开发者 2012 年 2 月 27 日”。
  • 在 NUnit 测试中,它是 C# 类库,既不工作Debug.WriteLine也不Trace.WriteLine工作。我已经运行了几次,可以确认这一点。我不会在这里发布我的测试代码,因为它很长并且需要大量的库才能工作,而且,它是由与https://pastebin.com/b7P0bYEa相同的故障引起的。

总而言之,如何从类 lib 或 win 表单写入 Visual Studio 输出窗口?

Fle*_*her 5

要同时使用Debug.WriteLineTrace.WriteLine我们需要 add using System.Diagnostics,它仅在调试(F5)时有效。即使是C#控制台项目,我们在Start without Debugging (Ctrl + F5) 时仍然无法获取Debug.WriteLineTrace.WriteLine输出,要查看输出,我们必须在Debugging (F5) 中运行它。

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

更新:

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

    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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我的 unite test 项目代码很简单:

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


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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在运行测试方法后,我可以得到输出: 在此处输入图片说明