我正在使用 C# 和 Visual Studio 2010。
当我使用OutputDebugString
编写调试信息时,它是否应该显示在输出窗口中?
我可以OutputDebugString
在DebugView中看到输出,但我想我会在 Visual Studio 的输出窗口中看到它。我在菜单工具下查看过?选项?调试?General,并且输出不会被重定向到立即窗口。我还查看了菜单 Tools* ?选项?调试?输出窗口和所有常规输出设置都设置为“开”。最后,我使用了输出窗口中的下拉列表来指定应该出现调试消息。
如果我更改菜单工具*?选项?调试?一般将输出重定向到即时窗口,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.WriteLine
显示OutputDebugString
在 DebugView 中。
为什么OutputDebugString
输出永远不会出现在输出窗口中?
最终,我的意图不是用 编写大量调试输出OutputDebugString
,而是使用 System.Diagnostics 或 NLog 或类似的东西。我只是想知道,如果我将日志平台配置为写入OutputDebugString
,输出是否可以从调试器中看到。
我回到了我的原始程序(不是上面的简单测试),它通过文件使用TraceSources
和TraceListeners
配置。app.config
如果我将跟踪源配置为写入System.Diagnostics.DefaultTraceListener
(记录为写入OutputDebugString
),那么跟踪源输出将进入调试窗口。但是,直接使用OutputDebugString
(例如在我的简单示例中)写入的行不会进入调试窗口。此外,如果我使用不同TraceListener
的写入方式OutputDebugString
(我从 Codeplex 的 Ukadc.Diagnostics 获得了一个),则该输出不会进入调试窗口。
关于 Ukadc.Diagnostics 跟踪侦听器的注意事项... Ukadc.Diagnostics 包含一些跟踪侦听器,允许自定义输出格式(类似于 log4net、NLog 和 LAB 中可用的格式)。因此,“仅”依赖于 Ukadc.Diagnostics 可以使用“标准”.NET 诊断日志记录,但我可以获得一些高级功能(如输出格式),而无需依赖可能更大的平台。在这种情况下,我可以使用 Ukadc.DiagnosticsOutputDebugStringTraceListener
以与写入文件时相同的格式(如果需要,或不同的格式)将日志输出写入调试窗口。
请注意,我已经看到了这些问题,但它们没有提供有效的解决方案: