简体   繁体   English

写入 Visual Studio 的输出窗口

[英]Writing to output window of Visual Studio

I am trying to write a message to the output window for debugging purposes.我正在尝试将消息写入输出窗口以进行调试。 I searched for a function like Java's system.out.println("") .我搜索了一个类似于 Java 的system.out.println("")的函数。 I tried Debug.Write , Console.Write , and Trace.Write .我尝试了Debug.WriteConsole.WriteTrace.Write It does not give an error, but it does not print anything either.它不会给出错误,但也不会打印任何内容。

"Define DEBUG constant" and "Define TRACE constant" options are checked.选中“定义调试常量”和“定义跟踪常量”选项。

Menu ToolsOptionsDebugging"Redirect all Output Window text to the Immediate Window" option is not checked.菜单工具选项调试“将所有输出窗口文本重定向到立即窗口”选项未选中。

Configuration: Active (Debug)配置:活动(调试)

Note: I created a project with the wizard as "Windows Forms Application" if relevant.注意:如果相关,我使用向导创建了一个项目为“Windows 窗体应用程序”。 I have no idea where to look.我不知道去哪里看。

Add the System.Diagnostics namespace, and then you can use Debug.WriteLine() to quickly print a message to the output window of the IDE.添加System.Diagnostics命名空间,然后您可以使用Debug.WriteLine()将消息快速打印到 IDE 的输出窗口。 For more details, please refer to these:有关更多详细信息,请参阅以下内容:

This will write to the debug output window:这将写入调试输出窗口:

using System.Diagnostics;

Debug.WriteLine("Send to debug output.");

使用:

System.Diagnostics.Debug.WriteLine("your message here");
Debug.WriteLine

is what you're looking for.就是你要找的。

If not, try doing this:如果没有,请尝试这样做:

Menu ToolsOptionsDebugging → uncheck Send Output to Immediate .菜单工具选项调试→ 取消选中Send Output to Immediate

For me, only the Trace namespace and not the Debug one worked:对我来说,只有Trace命名空间而不是 Debug 命名空间有效:

System.Diagnostics.Trace.WriteLine("message");

I'm working in a C# project under Visual Studio 2010.我正在 Visual Studio 2010 下的 C# 项目中工作。

You may be looking for您可能正在寻找

MessageBox.Show()

or或者

Debug.Writeline()

The call电话

System.Diagnostics.Debug.WriteLine("message");

fails when working with .NET Core (V 1.0 or 1.1).使用.NET Core (V 1.0 或 1.1)时失败。

We are supposed to create and use a logger from Microsoft.Extensions.Logging , but that log only appears in the dotnet.exe popup console window, not in Visual Studio's Output window.我们应该从Microsoft.Extensions.Logging创建和使用记录器,但该日志仅出现在 dotnet.exe 弹出控制台窗口中,而不出现在 Visual Studio 的输出窗口中。

This requires a third party framework, namely Serilog , but I've nonetheless found it to be a very smooth experience with getting output to some place I can see it.这需要第三方框架,即Serilog ,但我仍然发现将输出输出到我可以看到的地方是一种非常流畅的体验。

You first need to install Serilog's Trace sink .您首先需要安装 Serilog 的Trace sink Once installed, you need to set up the logger like this:安装后,您需要像这样设置记录器:

Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Trace()
    .CreateLogger();

(You can set a different minimum level or set it to a config value or any of the normal Serilog functionality. You can also set the Trace logger to a specific level to override configs, or however you want to do this.) (您可以设置不同的最低级别或将其设置为配置值或任何正常的 Serilog 功能。您还可以将Trace记录器设置为特定级别以覆盖配置,或者您想这样做。)

Then you just log messages normally and they show up in your Output window:然后您只需正常记录消息,它们就会显示在您的输出窗口中:

Logger.Information("Did stuff!");

This doesn't seem like such a big deal, so let me explain some additional advantages.这似乎没什么大不了的,所以让我解释一些额外的优势。 The biggest one for me was that I could simultaneously log to both the Output window and the console :对我来说最大的一个是我可以同时登录到输出窗口和控制台

Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Trace()
    .WriteTo.Console(standardErrorFromLevel: LogEventLevel.Error)
    .CreateLogger();

This gave me great flexibility in terms of how I consumed output, without having to duplicate all my calls to Console.Write with Debug.Write .这在我如何使用输出方面给了我很大的灵活性,而不必使用Debug.Write复制我对Console.Write所有调用。 When writing the code, I could run my command line tool in Visual Studio without fear of losing my output when it exited.编写代码时,我可以在 Visual Studio 中运行我的命令行工具,而不必担心退出时会丢失输出。 When I had deployed it and needed to debug something (and didn't have Visual Studio available), the console output was readily available for my consumption.当我部署它并需要调试某些东西时(并且没有可用的 Visual Studio),控制台输出随时可供我使用。 The same messages can also be logged to a file (or any other kind of sink) when it's running as a scheduled task.当它作为计划任务运行时,也可以将相同的消息记录到文件(或任何其他类型的接收器)中。

The bottom line is that using Serilog to do this made it really easy to dump messages to a multitude of destinations, ensuring I could always readily access the output regardless of how I ran it.最重要的是,使用 Serilog 执行此操作可以非常轻松地将消息转储到多个目的地,确保我始终可以轻松访问输出,无论我如何运行它。

It also requires very minimal set up and code.它还需要非常少的设置和代码。

This is not an answer to the original question.这不是原始问题的答案。 But since I found this question when searching for a means of interactively dumping object data, I figured others may benefit from mentioning this very useful alternative.但是由于我在搜索交互式转储对象数据的方法时发现了这个问题,我认为其他人可能会从提到这个非常有用的替代方案中受益。

I ultimately used the Command Window and entered the Debug.Print command, as shown below.我最终使用了Command Window,输入了Debug.Print命令,如下图。 This printed a memory object in a format that can be copied as text, which is all I really needed.这以一种可以复制为文本的格式打印了一个内存对象,这正是我真正需要的。

> Debug.Print <item>

  id: 1
  idt: null
  igad: 99
  igbd: 99
  gl_desc: "New #20"
  te_num: "1-001-001-020"

For debugging purposes, the System.Diagnostics.Debug.WriteLine() command will not be compiled into the release version of your code unless you have debug listeners.出于调试目的,除非您有调试侦听器,否则System.Diagnostics.Debug.WriteLine()命令不会编译到代码的发布版本中。 It writes to all trace listeners which includes the VS output window when running in Debug mode.在调试模式下运行时,它会写入所有跟踪侦听器,其中包括 VS 输出窗口。

For a Console application.对于控制台应用程序。 Console.WriteLine() would work but the output would still be generated in the release version of your binary. Console.WriteLine()可以工作,但输出仍会在二进制文件的发行版中生成。

Debug output should also appear in the normal output window when debugging tests;调试测试时,调试输出也应该出现在正常输出窗口中; whereas, console.writeline output does not (but can be found in the test output window.)而 console.writeline 输出没有(但可以在测试输出窗口中找到。)

Debug.Print("text here")

或者

Console.WriteLine("text here")

打印到Visual Studio的输出窗口:

Debug.Writeline();

The following worked for me in Visual Studio 2015:以下在 Visual Studio 2015 中对我有用:

OutputDebugStringW(L"Write this to Output window in VS14.");

Read the documentation for OutputDebugStringW here .此处阅读 OutputDebugStringW 的文档。

在此处输入图片说明 Note that this method only works if you are debugging your code ( debug mode )请注意,此方法仅适用于调试代码( debug mode

对于使用 NCrunch 的任何人,调试输出被重定向到 NCrunch 的“跟踪输出”窗口。

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

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