简体   繁体   English

我应该如何在C#中使用debug.print

[英]How should I use debug.print in C#

In VB, we can use debug.print to print the text in a console, but how do we do the similar thing in C#? 在VB中,我们可以使用debug.print在控制台中打印文本,但是如何在C#中执行类似的操作? The objective is to test the written class. 目的是测试书面课程。

Use Debug.WriteLine to write debug output. 使用Debug.WriteLine编写调试输出。

System.Diagnostics.Debug.WriteLine("My text");
Debug.WriteLine("My text"); //assuming you have using System.Diagnostics

The output will be displayed in your IDE. 输出将显示在您的IDE中。 Assuming Visual Studio, it will write to the Output window when you run the project. 假设使用Visual Studio,则在运行项目时它将写入“输出”窗口。 If you don't see the Output Window try View -> Output from the menu bar. 如果看不到“输出窗口”,请尝试从菜单栏中View -> Output When you see the Output window, make sure "Show Output From Debug " is selected. 当您看到“输出”窗口时,请确保已选择“显示来自调试的输出”。

You're looking for System.Diagnostics.Debug and/or System.Diagnostics.Trace . 您正在寻找System.Diagnostics.Debug和/或System.Diagnostics.Trace Pay attention to the difference (normally) between debug and release configuration. 注意调试和发布配置之间的区别(通常)。

Furthermore you might want to check out a logging framework like nlog or log4net. 此外,您可能想检查一下nlog或log4net之类的日志记录框架。

SolutionsCS - that will write to the Console, not the Debug Console. SolutionsCS-将写入控制台,而不是调试控制台。 If you then run the code in a non-debug environment it will still print out, which isn't really what you want for debugging messages. 如果您随后在非调试环境中运行代码,它仍会打印出来,这实际上不是调试消息所需要的。

Debug messages appear in the Output window if you select "Show Output From Debug". 如果选择“显示调试输出”,则调试消息将出现在“输出”窗口中。

If you want to see your debug messages without all the other cruft go to Tools->Options->Debugging->General and check Redirect all Output Window text to the Intermediate Window and, if the Intermediate Window is closed, follow xPert_Umer's instructions. 如果您想在没有其他所有杂项的情况下查看调试消息,请转到Tools->Options->Debugging->General然后选中Redirect all Output Window text to the Intermediate Window ,如果中间窗口关闭,请按照xPert_Umer的说明进行操作。

You can use Debug.WriteLine or Console.WriteLine . 您可以使用Debug.WriteLineConsole.WriteLine

Or write appropriate unit tests. 或编写适当的单元测试。

You can use System.Diagnostics.Debug.WriteLine to write text to the debug output. 您可以使用System.Diagnostics.Debug.WriteLine将文本写入调试输出。 When you compile in release mode, these calls will be removed. 在发布模式下编译时,这些调用将被删除。

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

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