首页 文章

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

提问于
浏览
18

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

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

我可以在DebugView中看到 OutputDebugString 的输出,但我想我会在Visual Studio的“输出”窗口中看到它 . 我看过菜单工具?选项?调试?常规,输出未被重定向到立即窗口 . 我也看了菜单工具*?选项?调试?输出窗口和所有常规输出设置均设置为"On" . 最后,我使用了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 写的行(例如在我的简单示例中)不要进入调试窗口 . 另外,如果我使用不同的 TraceListener 写入 OutputDebugString (我从Codeplex的Ukadc.Diagnostics获得了一个),那么该输出不会进入调试窗口 .

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

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

Herehere

3 回答

  • 3

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

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

  • 34

    在调试(Debug => Start Debugging F5)时,设置Project Properties,Debug选项卡,选中“Enable unmanaged code debugging”可以很好地工作 .

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

  • 8

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

    • 转到工具/选项/调试/常规 . 取消选中"Redirect all Output Window text to the Immediate Window"

    或者像那样的人 .

相关问题