以下C#程序(内置csc hello.cs)仅Hello via Console!在控制台和Hello via OutputDebugStringDebugView窗口中打印.但是,我看不到任何一个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" );
}
}
Run Code Online (Sandbox Code Playgroud)
是否需要一些特殊的命令行开关csc?
我没有使用Visual Studio进行任何开发,这是纯粹的命令行.
小智 110
虽然调试System.Diagnostics.Debug.WriteLine将显示在输出窗口(Ctrl+ Alt+ O)中,但您还可以TraceListener向Debug.Listeners集合中添加一个指定Debug.WriteLine其他位置的输出调用.
注意:Debug.WriteLine如果在菜单工具 → 选项 → 调试 → 常规下选中了Visual Studio选项"将所有输出窗口文本重定向到立即窗口",则调用可能不会显示在输出窗口中.要显示" 工具 → 选项 → 调试 ",请选中" 工具 → 选项 → 显示所有设置 " 旁边的框.
Tor*_*kår 73
正如其他人所指出的那样,必须注册听众才能阅读这些流.另请注意,Debug.Write只有在DEBUG设置了构建标志时Trace.Write才会起作用,而只有在设置了构建标志时才会起作用TRACE.
在Visual Studio中的项目属性中轻松完成设置DEBUG和/或TRACE标志,或者通过向csc.exe提供以下参数
/define:DEBUG;TRACE
jas*_*son 41
您需要添加一个TraceListener以查看它们出现在控制台上.
TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);
Run Code Online (Sandbox Code Playgroud)
在调试模式下,它们也出现在Visual Studio输出窗口中.