![](/img/trans.png)
[英]Capture System.Diagnostics.Debug Output in a WCF application
[英]Cs-Script & System.Diagnostics.Debug : Overriding Debug output traces
好吧,我认为标题可能很混乱...... :)
我的应用程序使用 CS-Script 作为脚本接口。 在我的应用程序将运行的脚本中,我想要某种“调试打印”——这意味着脚本编写者可以在脚本中的某个地方执行“Debug.Print("BLAAAAH;.!");") 并且会显示在我非常时髦的 output 对话框中的某个地方。
当然,我可以创建自己的调试输出工具,但由于我使用 C# 作为脚本语言,而使用 C# 的人将用于使用 System.Diagnostics 并使用 Debug.Print("..."),它如果我可以重新路由 System.Diagnostics.Debug.Print("") 和 System.Diagnostics.Debug.WriteLine("...") 以将它们的 output 跟踪到我很酷的输出窗口,那就太好了。
所以。
有谁知道是否可以将 C# 的 System.Diagnostic.Debug print/writeline output 重新路由到我可以控制的东西?
为此,您可以创建一个自定义TraceListener
。
您应该设置编译时调试符号或使用 /dbg 选项运行脚本
cscs /dbg <yourScript>
您还需要创建一个自定义TraceListener或简单地使用像 sysinternals DebugView这样的跟踪查看器
我也为此使用 TraceListener,但这是我的代码片段:
课程:
using System.Diagnostics;
public class DebugOutputListener : TraceListener
{
public event EventHandler<DebugMessageArgs> DebugMessage;
public override void Write(string message)
{
EventHandler<DebugMessageArgs> h = DebugMessage;
if (h != null)
{
DebugArgs args = new DebugArgs
{
Message = message
};
h(this, args);
}
}
public override void WriteLine(string message)
{
Write(message + "\r\n");
}
}
public class DebugMessageArgs : EventArgs
{
public string Message
{
get;
set;
}
}
要接收调试消息,请实例化 DebugOutputListener 的实例,订阅 DebugMessage 事件处理程序并将侦听器注册到 Debug.Listeners 集合。
例如
private void InitialiseDebugListener()
{
DebugListener dl = new DebugListener();
dl.DebugMessage += new EventHandler<DebugArgs>(Console_OnDebugMessage);
Debug.Listeners.Add(dl);
}
private void Console_OnDebugMessage(object sender, DebugMessageArgs e)
{
string debugMessage = e.Message;
// Do what you want with debugMessage.
// Be aware this may not come in on the application/form thread.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.