[C#] Redirect WriteLine stream to a textblock
Debug.WriteLine
// Release 모드로 .exe실행시 실행X, Debug모드 실행파일로 실행시 실행O
class TextBoxTraceListener : TraceListener
{
private TextBox tBox;
public TextBoxTraceListener(TextBox box)
{
this.tBox = box;
}
public override void Write(string msg)
{
//allows tBox to be updated from different thread
tBox.Dispatcher.BeginInvoke(new Action(() =>
{
tBox.AppendText(msg);
})
);
}
public override void WriteLine(string msg)
{
Write(msg + "\r\n");
}
}
public partial class MainWindow : Window
{
public MainWindow() //MainWindow class 생성자
{
InitializeComponent();
TextBoxTraceListener tbtl = new TextBoxTraceListener(TextBox);//TextBox control 객체인자로 전달
Debug.Listeners.Add(tbtl);
}
}
https://stackoverflow.com/questions/10950732/redirect-debug-writeline-stream-to-a-textblock
Redirect Debug.WriteLine stream to a textblock
I would like to redirect the Debug stdout stream to a textblock. Is there an easy way to do that ? Thanks.
stackoverflow.com
Trace.WriteLine
// Release 모드에서 실행시 실행O, Debug모드 실행파일로 실행시 실행O
This allows you to distinct between several outputs. So you can use Console.Write for user output and trace for debuging purposes
https://stackoverflow.com/questions/42548412/difference-between-console-writeline-trace-writeline
Difference between Console.writeline() /trace.writeline()
What is the difference between Console.WriteLine() and Trace.WriteLine() ?
stackoverflow.com
Console.WriteLine
// Console에 출력.
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.Dispatcher.BeginInvoke(new Action(() =>
{
_output.AppendText(value.ToString());
})
); // When character data is written, append it to the text box.
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
public partial class MainWindow : Window
{
public MainWindow() //MainWindow class 생성자
{
InitializeComponent();
TextWriter _writer = null;
_writer = new TextBoxStreamWriter(txList);
Console.SetOut(_writer);
}
}
https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/
How to redirect the Console’s output to a TextBox in C#
First let’s create a VS Windows Application Project named “ConsoleRedirection”. In order to redirect the console’s output to a text box in a Windows Form, we need to create …
saezndaree.wordpress.com