Enterprise Library Trace to TextBox

  • Thread starter Thread starter Benny
  • Start date Start date
B

Benny

Is there a way to trace exceptions and messages captured with the
Enterprise Library to a custom destination like a text box or any
other control? If not can anyone think of a way to "hack" something
like this?
 
Is there a way to trace exceptions and messages captured with the
Enterprise Library to a custom destination like a text box or any
other control? If not can anyone think of a way to "hack" something
like this?

This works. I haven't tested whether the threadsafe part works, but
it's a start. Create a new windows forms application, and paste this
into the main form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace WindowsApplication11
{
public delegate void UpdateTextDelegate(string pString);

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private TextBoxWriter _sw;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(264, 232);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 278);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

_sw = new TextBoxWriter(textBox1);
Trace.Listeners.Add(new TextWriterTraceListener(_sw));
Trace.AutoFlush = true;


}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Trace.Indent();
Trace.WriteLine("Test");
Trace.Unindent();
}
}
public class TextBoxWriter : System.IO.TextWriter
{
TextBox _textBox;

public TextBoxWriter(System.Windows.Forms.TextBox pTextBox)
{
_textBox = pTextBox;
}


public override void WriteLine(string value)
{
object[] x = {value};
if(_textBox.InvokeRequired)
{
_textBox.Invoke(new UpdateTextDelegate(UpdateText),new object[]
{value});
}
else
{
_textBox.Text += value + "\r\n";
}

}

public override System.Text.Encoding Encoding
{
get
{
// TODO: Add TextBoxWriter.Encoding getter implementation
return null;
}
}
private void UpdateText(string pString)
{
_textBox.Text += pString + "\r\n";
}
}
}
 
Is there a way to trace exceptions and messages captured with the
Enterprise Library to a custom destination like a text box or any
other control? If not can anyone think of a way to "hack" something
like this?

This works. I haven't tested whether the threadsafe part works, but
it's a start. Create a new windows forms application, and paste this
into the main form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace WindowsApplication11
{
public delegate void UpdateTextDelegate(string pString);

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private TextBoxWriter _sw;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(264, 232);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 278);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

_sw = new TextBoxWriter(textBox1);
Trace.Listeners.Add(new TextWriterTraceListener(_sw));
Trace.AutoFlush = true;

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Trace.Indent();
Trace.WriteLine("Test");
Trace.Unindent();
}
}
public class TextBoxWriter : System.IO.TextWriter
{
TextBox _textBox;

public TextBoxWriter(System.Windows.Forms.TextBox pTextBox)
{
_textBox = pTextBox;
}

public override void WriteLine(string value)
{
object[] x = {value};
if(_textBox.InvokeRequired)
{
_textBox.Invoke(new UpdateTextDelegate(UpdateText),new object[]
{value});
}
else
{
_textBox.Text += value + "\r\n";
}

}

public override System.Text.Encoding Encoding
{
get
{
// TODO: Add TextBoxWriter.Encoding getter implementation
return null;
}
}
private void UpdateText(string pString)
{
_textBox.Text += pString + "\r\n";
}
}



}- Hide quoted text -

- Show quoted text -

With Ent library, you'd want to create a new class

public class TextBoxExceptionPublisher : IExceptionPublisher

Then use the TextBoxWriter in the Publish override (and add an entry
to your config in the exception section.

Now the hacky bit, if you can make sure your textbox is assigned to a
static variable on a class that is visible to your ExceptionPublisher
ctor, you can initialise the TextBoxWriter with the textbox from
there. Bit ugly I know. The ExceptionPublisher isn't created until an
exception is raised anyway.

I can try and knock up an example if you need, but I'm running out of
time today. Someone else might have a better idea though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top