可以在Visual Studio的输出窗口中查看OutputDebugString的输出吗?

我正在使用C#和Visual Studio 2010。

当我使用OutputDebugString写入调试信息时,它应该出现在输出窗口中吗?

我可以在DebugView中看到OutputDebugString的输出,但我想我会在Visual Studio的Output窗口中看到它。 我看过菜单工具选项调试常规 ,输出未被重定向到立即窗口。 我还看了菜单Tools *? 选项调试输出窗口和所有常规输出设置都设置为“开”。 最后,我使用了Output窗口中的下拉列表来指定应该出现Debug消息。

如果我更改菜单工具*? 选项调试一般将输出重定向到立即窗口, OutputDebugString消息不会出现在即时窗口中。

这是我的整个测试程序:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; namespace OutputDebugString { class Program { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern void OutputDebugString(string message); static void Main(string[] args) { Console.WriteLine("Main - Enter - Console.WriteLine"); Debug.WriteLine("Main - Enter - Debug.WriteLine"); OutputDebugString("Main - Enter - OutputDebugString"); OutputDebugString("Main - Exit - OutputDebugString"); Debug.WriteLine("Main - Exit - Debug.WriteLine"); Console.WriteLine("Main - Exit - Console.WriteLine"); } } } 

如果我在调试器中运行, Debug.WriteLine输出确实显示在输出窗口中,但OutputDebugString输出不显示。

如果我从控制台窗口运行,则Debug.WriteLineOutputDebugString显示在DebugView中。

为什么OutputDebugString输出不会出现在输出窗口中?

最终,我的意图不是用OutputDebugString编写大量的调试输出,而是使用System.Diagnostics或NLog或类似的东西。 我只是试图找出,如果我配置一个日志记录平台写入OutputDebugString ,输出将从调试器中可见。

我回到了原始程序(不是上面的简单测试),它使用通过app.config文件配置的TraceSourcesTraceListeners 。 如果我将跟踪源配置为写入System.Diagnostics.DefaultTraceListener (记录为写入OutputDebugString ),则跟踪源输出DOES将转到调试窗口。 但是,使用OutputDebugString直接写入的行(例如在我的简单示例中)不要进入调试窗口。 另外,如果我使用写入OutputDebugString的不同TraceListener (我在Codeplex从Ukadc.Diagnostics获得一个),那么该输出不会进入调试窗口。

关于Ukadc.Diagnostics跟踪侦听器的一个注释… Ukadc.Diagnostics包含一些跟踪侦听器,允许自定义输出格式(类似于log4net,NLog和LAB中可用的格式)。 因此,“仅”依赖于Ukadc.Diagnostics,可以使用“标准”.NET诊断日志记录,但我可以获得一些高级function(如输出格式),而不依赖于可能更大的平台。 在这种情况下,我可以使用Ukadc.Diagnostics OutputDebugStringTraceListener以与写入文件时相同的格式(如果需要或不同的格式)将日志输出写入调试窗口。

请注意,我已经看到了这些问题,但它们没有提供可行的解决方案:

这里和这里

你让我讨论这个问题一段时间了。 没门! 办法。

单击项目>属性>调试选项卡,打开“启用非托管代码调试”复选框。 在以后的VS版本中重命名为“启用本机代码调试”。 启用非托管代码调试引擎后,OutputDebugString()输出现在可以正确拦截并定向到“输出”窗口。

在调试(Debug => Start Debugging F5)时,设置Project + Properties,Debug选项卡,选中“启用非托管代码调试”可以很好地工作。

当不调试(Debug => Start Without Debugging CTRL + F5)时,你最好使用SysInternals库中的DebugView。 下载DebugView for Windows v4.76

由于设置,它可能会在“立即窗口”中显示:

  • 转到工具/选项/调试/常规。 取消选中“将所有输出窗口文本重定向到立即窗口”

或者像那样的人。