完全按照“Writing to output window of Visual Studio?”中的要求,但是解决了剩余的问题 --
-
I assume there is no way to write to output if i start without debugging (ctrl-f5) right? – previous_developer Feb 27 '12
-
Debug.WriteLine() will only work when running in Debug. That means running it with F5 and not CTRL-F5. – kirk.burleson Jul 15 '13
然而,事实并非如此,因为:
-
Just ran a small app here, works fine for me. Perhaps there is a small glitch in your environment? – Bhargav Bhat Feb 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) ==========
要在 Debug模式下运行,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) ==========
即,无论正常运行还是在 Debug模式下,NUnit Adapter 都能够写入 Visual Studio 输出窗口,区别仅在于文本“Running selected tests”或“调试选定的测试"
所以,这里是正常运行,非 Debug模式调试输出案例的总结:
- 对于 C# 控制台,
Debug.WriteLine
和Trace.WriteLine
都可以正常工作。我已经创建并验证了这个:https://pastebin.com/Du6ZbDV3 - 但是对于没有控制台的类/表单等,
Debug.WriteLine
和Trace.WriteLine
都不起作用。示例:https://pastebin.com/b7P0bYEa . “它非常简单,但仍然一无所获 – previous-developer 2012 年 2 月 27 日”。 - 在 NUnit 测试中,它是 C# 类库,
Debug.WriteLine
和Trace.WriteLine
都不起作用。我已经运行了几次,可以确认这一点。我不会在这里发布我的测试代码,因为它很长并且需要很多库才能工作,而且,它是由与 https://pastebin.com/b7P0bYEa 相同的故障引起的。 .
总而言之,如何从类 lib 或 win 窗体写入 Visual Studio 输出窗口?
最佳答案
要同时使用 Debug.WriteLine
和 Trace.WriteLine
我们需要添加 using System.Diagnostics
,它只在调试时有效(F5 ).即使它是一个 C# 控制台项目 我们仍然无法在 Start without Debugging (Ctrl + F5) 时得到 Debug.WriteLine
和 Trace.WriteLine
输出,到查看我们必须在 调试 (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 添加到引用中
我的联合测试项目代码很简单:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;
namespace UnitTestProject3
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
hello.foo();
}
}
}
关于c# - 写入 Visual Studio 输出窗口,万无一失的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192936/