区分Tracelistener中的跟踪和调试消息

时间:2015-01-27 11:15:03

标签: c# trace tracelistener

我有一个程序可以测试不同程序的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还是跟踪?

谢谢, 锐衡

1 个答案:

答案 0 :(得分:0)

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

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();
    }

}
}