1

我想选择在文本框、调试和/或跟踪输出上显示的内容。我怎样才能做到这一点?

在示例代码中,我只将输出添加到 Trace.Listeners,但 Debug.Writeline 仍然出现在文本框中。为什么?

预期结果:控制台跟踪

实际结果:控制台跟踪 调试

示例代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        TextBoxWriter txtWriter = new TextBoxWriter(this.txbMultiLine);
        Console.SetOut(txtWriter);

        var traceDebugOutput = new TextWriterTraceListener(txtWriter);
        Trace.Listeners.Add(traceDebugOutput);

        Console.WriteLine("Console");
        Trace.WriteLine("Trace");
        Debug.WriteLine("Debug");
    }
}

public class TextBoxWriter : TextWriter
{
    TextBox _output = null;

    public TextBoxWriter(TextBox output)
    {
        _output = output;
    }

    public override void Write(char value)
    {
        base.Write(value);
        _output.AppendText(value.ToString());
    }

    public override Encoding Encoding
    {
        get { return System.Text.Encoding.UTF8; }
    }
}
4

0 回答 0