Trace Listener to Write to a Text Box (Wpf Application)

Trace listener to write to a text box (WPF application)

I use this for C# winforms, should be easily adjustable to wpf

public class MyTraceListener : TraceListener
{
private TextBoxBase output;

public MyTraceListener(TextBoxBase output) {
this.Name = "Trace";
this.output = output;
}

public override void Write(string message) {

Action append = delegate() {
output.AppendText(string.Format("[{0}] ", DateTime.Now.ToString()));
output.AppendText(message);
};
if (output.InvokeRequired) {
output.BeginInvoke(append);
} else {
append();
}

}

public override void WriteLine(string message) {
Write(message + Environment.NewLine);
}
}

Use it like

TraceListener debugListener = new MyTraceListener (theTextBox);
Debug.Listeners.Add(debugListener);
Trace.Listeners.Add(debugListener);

Remember to Trace/Debug.Listeners.Remove(debugListener); when you don't need it anymore.

How to output Debug Information to a RichTextBox?

You can implement your own TraceListener that gets configured in the app.config and dynamically tries to find a richtextbox that matches the name in the config.

Your class can look like this:

public class TextBoxListener : TraceListener
{
RichTextBox _box;
string _data;
public TextBoxListener(string initializeData)
{
_data = initializeData;
}

private bool Init()
{
if (_box != null && _box.IsDisposed )
{
// back to null if the control is disposed
_box = null;
}
// find the logger text box
if (_box == null)
{
// open forms
foreach (Form f in Application.OpenForms)
{
// controls on those forms
foreach (Control c in f.Controls)
{
// does the name match
if (c.Name == _data && c is RichTextBox)
{
// found one!
_box = (RichTextBox) c;
break;
}
}
}
}
return _box != null && !_box.IsDisposed;
}

public override void WriteLine(string message)
{
if (Init())
{
_box.Text = _box.Text + message + "\r\n";
}
}

public override void Write(string message)
{
if (Init())
{
_box.Text = _box.Text + message;
}
}
}

Most important part of this class is the Init method that iterates over all openforms and all controls to find an richtextbox control that matches the name that is configured in the app.config.

To make use of this class configure tracing in your app.config and add a listener

<system.diagnostics>
<trace>
<listeners>
<add name="box"
type="WindowsFormsApplication1.TextBoxListener, WindowsFormsApplication1"
initializeData="loggerRTB" />
</listeners>
</trace>
</system.diagnostics>

The type is the fully qualified typename of your class (Namespace.Classname, AssemblyName) and in initializeData you'll add the name of your richtextbox control on your form. In my example app it is loggerRTB.

In your app you can use the standard Trace class now:

Trace.WriteLine("Hello world");

Notice that the TextBoxListener is only instantiated on the first call to Trace.WriteLine.

One of the benefits of this approach is the possibility to add multiple Listeners that write to different RichTextBox controls, even on separate forms (assuming those forms are open).

Trace listener to write to a text box (WPF application)

I use this for C# winforms, should be easily adjustable to wpf

public class MyTraceListener : TraceListener
{
private TextBoxBase output;

public MyTraceListener(TextBoxBase output) {
this.Name = "Trace";
this.output = output;
}

public override void Write(string message) {

Action append = delegate() {
output.AppendText(string.Format("[{0}] ", DateTime.Now.ToString()));
output.AppendText(message);
};
if (output.InvokeRequired) {
output.BeginInvoke(append);
} else {
append();
}

}

public override void WriteLine(string message) {
Write(message + Environment.NewLine);
}
}

Use it like

TraceListener debugListener = new MyTraceListener (theTextBox);
Debug.Listeners.Add(debugListener);
Trace.Listeners.Add(debugListener);

Remember to Trace/Debug.Listeners.Remove(debugListener); when you don't need it anymore.

Redirect Debug.WriteLine stream to a textblock

It seems there is no solution for WP7.
I guess I must wait the next version.



Related Topics



Leave a reply



Submit