C# System.Diagnostics.Debug.Write输出显示在哪里?
下面的C#程序(使用C# System.Diagnostics.Debug.Write输出显示在哪里?,c#,debugging,C#,Debugging,下面的C#程序(使用csc hello.cs构建)通过控制台只打印hello和DebugView窗口中的OutputDebugString中的Hello。但是,我看不到任何一个System.Diagnostics.*调用。为什么呢 using System; using System.Runtime.InteropServices; class Hello { [DllImport("kernel32.dll", CharSet=CharSet.Auto)] public sta
csc hello.cs
构建)通过控制台只打印hello控制台上的code>和DebugView窗口中的OutputDebugString中的Hello。但是,我看不到任何一个System.Diagnostics.*
调用。为什么呢
using System;
using System.Runtime.InteropServices;
class Hello {
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern void OutputDebugString(string message);
static void Main() {
Console.Write( "Hello via Console!" );
System.Diagnostics.Debug.Write( "Hello via Debug!" );
System.Diagnostics.Trace.Write( "Hello via Trace!" );
OutputDebugString( "Hello via OutputDebugString" );
}
}
csc
是否需要一些特殊的命令行开关
我没有使用Visual Studio进行任何开发,这是纯命令行的东西。在Visual Studio中调试时,显示“输出”窗口(查看->输出)。它将显示在那里。诊断消息将显示在输出窗口中。您需要添加一个TraceListener
以查看它们出现在控制台上
TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);
当处于调试模式时,它们也会出现在Visual Studio输出窗口中。正如其他人所指出的,必须注册侦听器才能读取这些流。还请注意,Debug.Write
仅在设置了Debug
build标志时才起作用,而Trace.Write
仅在设置了Trace
build标志时起作用
设置调试
和/或跟踪
标志可以在Visual Studio的项目属性中轻松完成,或者通过向csc.exe提供以下参数来完成
/define:调试;跟踪
调试System.Diagnostics.Debug.WriteLine
时,输出窗口(Ctrl+Alt+O)中将显示,您还可以向Debug.Listeners
集合添加TraceListener
以指定在其他位置进行的Debug.WriteLine
调用
注意:Debug.WriteLine
如果在“工具”菜单下选中Visual Studio选项“将所有输出窗口文本重定向到即时窗口”,则输出窗口中可能不会显示调用→ 选择权→ 调试→ 将军。显示“工具”→ 选择权→ “调试”,选中“工具”旁边的框→ 选择权→ 显示所有设置。当我在代码中写入debug.write(“”)时,它在“即时窗口”中输出,而不是在“输出窗口”中输出
你可以试试。用于显示“立即”窗口(调试→ 窗口→ 立即)。对于VB.NET,以下内容适用。您必须选择“调试”并确保“开始调试”。按F5可达到此目的
此外,Console.WriteLine仅在输出窗口中构建为“释放”时显示消息
如前所述,使用视图打开输出窗口→ 如果要查看Console.WriteLine消息,请输出并确保选择“Build”,如果要查看Debug.WriteLine或Trace.WriteLine消息,请选择“Debug”。我的案例解决方案是:
右键单击输出窗口李>
检查“程序输出”
发现Debug和Trace对象使用相同的跟踪侦听器对我很有用(没想到),因此Debug.Write和Trace.Write输出都在同一个位置,只是根据可能无法执行的条件,您能详细说明它们是什么条件吗?具体哪种情况下,debug.Write与trace.Write不一样?这对我不起作用,我在vs项目属性中设置了debug和trace标志,并且正在使用debug.WriteLine,执行该行,但输出窗口中没有显示任何内容,有人得到任何其他建议吗?要展开@hello_earth comment,请参阅DebugView将捕获.NET Debug.Write()和Win32 OutputDebugString():@dlchambers:我认为这不正确。该页面声称显示了OutputDebugString()
和(内核)DbgPrint();我发现DebugView可以捕获Debug.Write()
,如果它的捕获设置包括“全局Win32”-这需要在管理模式下运行它。我想确认(Ctrl+Alt+O)将在Visual Studio 2012中调试时显示输出窗口。另一个回复中的一些评论提到,可以使用Microsoft的(系统内部)调试视图: