【问题标题】:Distinguish between trace and debug messages in a Tracelistener在 Tracelistener 中区分跟踪和调试消息
【发布时间】:2015-03-25 23:47:31
【问题描述】:

我有一个程序可以测试不同程序的 c# 脚本。当编写脚本时,可以使用Debug.Writeline或Trace.Writeline方法并将文本写入richtextbox,我希望不同方法的文本具有不同的颜色,Debug为橙色,Trace为蓝色。

就在我运行脚本之前

System.Diagnostics.Trace.Listeners.Add(new TextboxTraceListener(outputTxt, "ScriptTraceListener", Color.DarkBlue));
System.Diagnostics.Debug.Listeners.Add(new TextboxTraceListener(outputTxt, "ScriptDebugListener", Color.DarkOrange));

脚本运行后是

System.Diagnostics.Trace.Listeners.Remove("ScriptTraceListener");
System.Diagnostics.Debug.Listeners.Remove("ScriptDebugListener");

TextBoxTraceListener 是

class TextboxTraceListener : TraceListener
{

    private DebugRichTextbox output;
    private Color textColor;

    public TextboxTraceListener(DebugRichTextbox output) :this(output, "RichTextboxTraceListener", Color.Black)
    {
    }

    public TextboxTraceListener(DebugRichTextbox output, string name, Color textColor)
    {
        this.Name = name;
        this.output = output;
        this.textColor = textColor;
    }


    public override void Write(string message)
    {
        Action write = delegate() { output.write(message, this.textColor); };
        if (output.InvokeRequired)
        {
            IAsyncResult result = output.BeginInvoke(write);
            output.EndInvoke(result);
        }
        else
        {
            write();
        }
    }

    public override void WriteLine(string message)
    {
        Action writeLine = delegate() { output.writeLine(message, this.textColor); };
        if (output.InvokeRequired)
        {
            IAsyncResult result = output.BeginInvoke(writeLine);
            output.EndInvoke(result);
        }
        else
        {
            writeLine();
        }

    }
}

似乎 Debug.Writeline 和 Trace.Writeline 消息被两个侦听器捕获,并且消息只是在蓝色和橙色的 Richtextbox 中重复。有没有办法判断消息是来自TextBoxTraceListener中的Debug还是trace?

谢谢, 欧根

【问题讨论】:

  • Trace 和 Debug 是相同的,只是 Trace 侦听器可以在 Debug 和 Release 模式下读取,但 Debug 侦听器不能在 Release 模式下读取。您可能有一个解决方法,提供仅使用 Trace 编写文本的自定义类(调试和跟踪),您可以区分它们类中的颜色。

标签: c# trace tracelistener


【解决方案1】:

我想出了这个。看起来不太好,但似乎可以工作。我只是添加了一个监听器,它是这个类的一个实例。如果有人有更好的解决方案,我会对此感兴趣。

class TextboxTraceListener : TraceListener
{
private DebugRichTextbox output;
private Color debugColor;
private Color traceColor;

public TextboxTraceListener(DebugRichTextbox output) :this(output, "RichTextboxTraceListener", Color.Black, Color.Black)
{
}

public TextboxTraceListener(DebugRichTextbox output, string name, Color debugColor, Color traceColor)
{
    this.Name = name;
    this.output = output;
    this.debugColor = debugColor;
    this.traceColor = traceColor;
}


public override void Write(string message)
{
    Color thisColor = Color.Black;
    StackTrace trace = new StackTrace();
    StackFrame[] stackFrames = trace.GetFrames();
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("TRACE")))
    {
        thisColor = traceColor;
    }
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("DEBUG")))
    {
        thisColor = debugColor;
    }

    Action write = delegate() { output.write(message, thisColor); };
    if (output.InvokeRequired)
    {
        IAsyncResult result = output.BeginInvoke(write);
        output.EndInvoke(result);
    }
    else
    {
        write();
    }
}

public override void WriteLine(string message)
{

    Color thisColor = Color.Black;
    StackTrace trace = new StackTrace();
    StackFrame[] stackFrames = trace.GetFrames();
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("TRACE")))
    {
        thisColor = traceColor;
    }
    if (Array.Exists(stackFrames, element => element.GetMethod().DeclaringType.Name.ToUpper().Equals("DEBUG")))
    {
        thisColor = debugColor;
    }

    Action writeLine = delegate() { output.writeLine(message, thisColor); };
    if (output.InvokeRequired)
    {
        IAsyncResult result = output.BeginInvoke(writeLine);
        output.EndInvoke(result);
    }
    else
    {
        writeLine();
    }

}
}

【讨论】:

  • 您对正在写入 Trace & Debug 的应用程序没有任何控制权?
  • 我可以控制应用程序,但该应用程序是用于编写 C# 脚本的 UI,有点像一个愚蠢的 IDE。这是写入 Trace & Debug 的脚本,我只是在侦听器启动之前附加它,并在它完成后将其删除。在这两点之间我可以控制的那部分代码只是编译在 UI 中编写的脚本并调用该程序集上的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 2011-04-19
  • 2017-06-03
  • 1970-01-01
  • 2012-10-03
  • 2011-10-21
  • 1970-01-01
相关资源
最近更新 更多