写入Visual Studio输出窗口,可靠的方式

时间:2018-01-10 17:26:29

标签: c# visual-studio debugging visual-studio-debugging

完全按照" Writing to output window of Visual Studio?"的要求,然而解决剩余问题 -

  •   

    我认为如果我没有调试(ctrl-f5)就开始没有办法写输出吗? - previous_developer 2月27日' 12

  •   

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

但事实并非如此,因为:

  •   

    刚刚在这里运行了一个小应用程序,对我来说很好。也许你的环境有一个小问题? - Bhargav Bhat 2月27日' 12

  • 此外,

我可以在正常运行期间看到在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) ==========

要在调试模式下运行,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 Adapter都能够写入Visual Studio输出窗口,区别仅在于文本" 运行选定的测试&#34 ;或" 调试选定的测试"

因此,以下是调试输出案例在正常运行期间未处于调试模式的摘要:

  • 对于C#控制台,Debug.WriteLineTrace.WriteLine都可以正常工作。我已经创建并验证了这一点:https://pastebin.com/Du6ZbDV3
  • 但是对于没有控制台的班级/表格等,Debug.WriteLineTrace.WriteLine都不起作用。示例:https://pastebin.com/b7P0bYEa。 " 这很简单,但仍然没有 - 前一个开发人员2月27日' 12 "。
  • 在NUnit测试中,即C#Class lib,Debug.WriteLineTrace.WriteLine都不起作用。我经常跑过几次,可以证实这一点。我不会在这里发布我的测试代码,因为它很长而且很长。需要大量的库来工作,而且,它是由与https://pastebin.com/b7P0bYEa相同的故障引起的。

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

1 个答案:

答案 0 :(得分:3)

要同时使用Debug.WriteLineTrace.WriteLine,我们需要添加using System.Diagnostics,这只有在调试时才有效(F5)。即使是 C#控制台项目,我们仍然无法在没有调试的情况下(Ctrl + F5)输出Debug.WriteLineTrace.WriteLine,以查看我们必须在< strong>调试(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");
        }
    }
}

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

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

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


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

        }
    }
}

然后在运行测试方法后我可以得到输出: enter image description here

相关问题