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

发布于 2024-09-05 10:33:07 字数 2786 浏览 6 评论 0原文

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

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

我可以在 DebugViewOutputDebugString 的输出>,但我想我会在 Visual Studio 的输出窗口中看到它。我看过 在菜单工具下? 选项调试常规,并且输出不会被重定向到立即窗口。我还查看了菜单“工具”*? 选项调试输出窗口和所有常规输出设置均设置为“打开”。最后,我使用“输出”窗口中的下拉列表来指定应显示调试消息。

如果我更改菜单工具*? 选项调试常规将输出重定向到立即窗口,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),则跟踪源输出会转到调试窗口。但是,直接使用 OutputDebugString 写入的行(例如在我的简单示例中)不会进入调试窗口。另外,如果我使用不同的 TraceListener 写入 OutputDebugString(我从 Codeplex 的 Ukadc.Diagnostics 获得了一个),则该输出不会进入调试窗口。

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

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

此处此处

I am using C# and Visual Studio 2010.

When I use OutputDebugString to write debug information, should it show up in the output window?

I can see the output from OutputDebugString in DebugView, but I thought I would see it in Visual Studio's Output window. I have looked
under menu Tools ? Options ? Debugging ? General, and the output is NOT being redirected to the Immediate window. I have also looked under menu Tools* ? Options ? Debugging ? Output Window and all General Output Settings are set to "On". Finally, I have used the drop-down list in the Output window to specify that Debug messages should appear.

If I change menu Tools* ? Options ? Debugging ? General to redirect the output to the Immediate window, the OutputDebugString messages do not appear in the immediate window.

Here is my entire test program:

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");
    }
  }
}

If I run within the debugger, the Debug.WriteLine output does show up in the output window, but the OutputDebugString output does not.

If I run from a console window, both Debug.WriteLine and OutputDebugString show up in DebugView.

Why doesn't the OutputDebugString output ever show up in the output window?

Ultimately, my intent is not to write a lot of debug output with OutputDebugString, rather I will use System.Diagnostics or NLog or something similar. I am just trying to find out, if I configure a logging platform to write to OutputDebugString, will the output be visible from within the debugger.

I went back to my original program (not the simple test above) which uses TraceSources and TraceListeners configured via the app.config file. If I configure the trace sources to write to the System.Diagnostics.DefaultTraceListener (which is documented as writing to OutputDebugString), then the trace source output DOES go to the debug window. However, lines that write directly with OutputDebugString (such as in my simple example) DO NOT go to the debug window. Also, if I use a different TraceListener that writes to OutputDebugString (I got one from Ukadc.Diagnostics at Codeplex), that output DOES NOT go to the debug window.

One note about the Ukadc.Diagnostics trace listener... Ukadc.Diagnostics contains some trace listeners that allow for custom formatting of output (similar to the formatting that is available in log4net, NLog, and LAB). So, with "only" a dependency on Ukadc.Diagnostics one can use "standard" .NET diagnostic logging, but I can get some advanced features (like the output formatting) without becoming dependent on a possibly much larger platform. In this case, I could use the Ukadc.Diagnostics OutputDebugStringTraceListener to write logging output to the debug window in the same format (if desired, or a different format) as it would be if written to a file.

Note that I have seen these questions, but they did not provide a working solution:

Here and here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如果没结果 2024-09-12 10:33:07

你让我问这个问题有一段时间了。决不!方式。

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

You had me going on this question for a while. No way! Way.

Project > Properties > Debug tab, turn on the "Enable unmanaged code debugging" checkbox. Renamed to "Enable native code debugging" in later VS versions. With the unmanaged code debugging engine enabled, OutputDebugString() output is now properly intercepted and directed to the Output window.

尘曦 2024-09-12 10:33:07

调试时(调试==>开始调试F5),设置
项目+属性,调试选项卡,选中“启用非托管代码调试”
效果很好。

当不进行调试时(Debug => Start Without Debugging CTRL+F5),您必须使用 SysInternals 库中的 DebugView。 下载 Windows 版 DebugView v4.76

When debuggging (Debug => Start Debugging F5), the setting
Project + Properties, Debug tab, check "Enable unmanaged code debugging"
works nicely.

When NOT debugging (Debug => Start Without Debugging CTRL+F5) you mujst use DebugView from the SysInternals library. Download DebugView for Windows v4.76

一绘本一梦想 2024-09-12 10:33:07

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

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

或类似的内容。

It might be showing in the "Immediate Window" instead due to a setting:

  • Go to Tools/Options/Debugging/General. Uncheck "Redirect all Output Window text to the Immediate Window"

Or somethig like that.

晌融 2024-09-12 10:33:07

我有项目>属性>调试选项卡>勾选了“启用非托管代码调试”,但即使断点起作用,OutputDebugString() 也不起作用。

我也有解决方案>共同属性>项目依赖关系>在我的 C# 项目下勾选了我的 DLL,但构建依赖项不起作用。

这两个问题的解决方法是解决方案 >配置属性>勾选“在我的 DLL 上构建”。

I had Project > Properties > Debug tab > ticked the "Enable unmanaged code debugging" but OutputDebugString() didn't work even though breakpoints did.

I had also Solution > Common Properties > Project Dependencies > ticked my DLL under my C# project but build dependencies didn't work.

The fix for both these issues was to Solution > Configuration Properties > tick build on my DLL.

故人的歌 2024-09-12 10:33:07

我曾经遇到过这个问题,并且该线程中建议的所有解决方案都失败了。 DebugView 能够看到调试消息,而 Visual Studio 2017 则不能,无论我如何尝试配置它。启用本机代码调试吗?查看。没有重定向到“立即”?查看。项目不排除在编译之外吗?查看。

最终,我在安装中使用“修复”选项,通过 Visual Studio 安装程序解决了问题。花了大约10分钟完成。

I had this problem once and all solutions suggested in this thread failed. DebugView was able to see debug messages, Visual Studio 2017 wasn't, no matter how I tried to configure it. Enable native code debugging? Check. No redirect to "immediate"? Check. Project is not excluded from compilation? Check.

Eventually I solved issue via Visual Studio Installer by using "Repair" option on my installation. Took about 10 minutes to complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文