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