简体   繁体   English

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

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

Exactly as what asked in " Writing to output window of Visual Studio? ", however solving the remaining issue -- 正如“ 写入Visual Studio的输出窗口? ”中提到的那样 ,然而解决剩下的问题 -

  • I assume there is no way to write to output if i start without debugging (ctrl-f5) right? 我假设如果我没有调试(ctrl-f5)就开始没有办法写输出吗? – previous_developer Feb 27 '12 - previous_developer 12年2月27日

  • Debug.WriteLine() will only work when running in Debug. Debug.WriteLine()仅在Debug中运行时才有效。 That means running it with F5 and not CTRL-F5. 这意味着使用F5而不是CTRL-F5运行它。 – kirk.burleson Jul 15 '13 - kirk.burleson 2013年7月15日

However, it is not the case because: 但事实并非如此,因为:

  • Just ran a small app here, works fine for me. 刚刚在这里运行了一个小应用程序,对我来说很好。 Perhaps there is a small glitch in your environment? 也许你的环境有一个小问题? – Bhargav Bhat Feb 27 '12 - Bhargav Bhat 2月27日

  • Moreover, 此外,

I can see some Informational printed in Visual Studio output window during normal run : 我可以在正常运行期间看到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) ==========

For to run in debug mode, The NUnit debug output will look like: 要在调试模式下运行,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) ==========

Ie, regardless normal run or in debug mode, NUnit Adapter is able to write to the Visual Studio output window, the difference is only in the text " Running selected tests " or " Debugging selected tests " 即,无论是正常运行还是在调试模式下,NUnit Adapter都能够写入Visual Studio输出窗口,区别仅在于文本“ 运行选定的测试 ”或“ 调试选定的测试

So, here is the summary of debugging output cases during normal run , not in debug mode : 因此,以下是正常运行期间调试输出情况的摘要, 而不是调试模式

  • For C# Console, both Debug.WriteLine and Trace.WriteLine works fine. 对于C#Console, Debug.WriteLineTrace.WriteLine可以正常工作。 I've created and verified with this: https://pastebin.com/Du6ZbDV3 我已经创建并验证了这个: https//pastebin.com/Du6ZbDV3
  • However for Class/Form etc that don't have Console, neither Debug.WriteLine nor Trace.WriteLine work. 但是对于没有Console的Class / Form等, Debug.WriteLineTrace.WriteLine不起作用。 Example: https://pastebin.com/b7P0bYEa . 示例: https//pastebin.com/b7P0bYEa " It is pretty simple but still nothing – previous-developer Feb 27 '12 ". 这很简单,但仍然没有 - 以前的开发人员2月27日'12 ”。
  • In NUnit testing, which is C# Class lib, neither Debug.WriteLine nor Trace.WriteLine works. 在NUnit测试中,它是C#Class lib, Debug.WriteLineTrace.WriteLineTrace.WriteLine I've run mine several times and can confirm this. 我跑过几次,可以证实这一点。 I'll not be posting my test code here because it is lengthy & needs lots of libs to work, moreover, it is caused by the same glitch as https://pastebin.com/b7P0bYEa . 我不会在这里发布我的测试代码,因为它很长并且需要大量的库来工作,而且,它是由与https://pastebin.com/b7P0bYEa相同的故障引起的。

So all in all, How to write to Visual Studio output window from class lib or win forms? 总而言之,如何从类lib或win表单写入Visual Studio输出窗口?

To using both Debug.WriteLine and Trace.WriteLine we need add using System.Diagnostics , which only works when it's debugging (F5). 要同时使用Debug.WriteLineTrace.WriteLine我们需要using System.Diagnostics添加,这只能在调试时使用(F5)。 Even it's a C# Console project we still cannot get Debug.WriteLine and Trace.WriteLine output when Start without Debugging (Ctrl + F5), to see the output we have to run it in Debugging (F5) . 即使它是一个C#Console项目,我们仍然无法在Start without Debugging(Ctrl + F5)时获得Debug.WriteLineTrace.WriteLine输出,以查看我们必须在Debugging(F5)中运行它的输出。

For Class library project, it could only be built or compiled , I don't think we could Start or run it as it's not a console project unless it's been called in a console project. 对于类库项目,它只能构建或编译 ,我认为我们不能启动或运行它,因为它不是控制台项目,除非它已在控制台项目中调用。

UPDATE: 更新:

To debug a class library project and show its output, I add a unit test in my solution in VS. 要调试类库项目并显示其输出,我在VS的解决方案中添加了一个单元测试。 My class library code: 我的类库代码:

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

In unit test project I added the ClassLibrary1 to the Reference 在单元测试项目中,我将ClassLibrary1添加到Reference

and my unite test project code is simple: 我的联合测试项目代码很简单:

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


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

        }
    }
}

Then after running the test method i could get the output: 然后运行测试方法后我可以得到输出: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM