On 15 Mar, 15:03, "Benny" <bennyandli...@ gmail.comwrote:
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.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Diagnost ics;
namespace WindowsApplicat ion11
{
public delegate void UpdateTextDeleg ate(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.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(8, 8);
this.textBox1.M ultiline = true;
this.textBox1.N ame = "textBox1";
this.textBox1.S crollBars =
System.Windows. Forms.ScrollBar s.Vertical;
this.textBox1.S ize = new System.Drawing. Size(264, 232);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(8, 248);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 278);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
_sw = new TextBoxWriter(t extBox1);
Trace.Listeners .Add(new TextWriterTrace Listener(_sw));
Trace.AutoFlush = true;
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
Trace.Indent();
Trace.WriteLine ("Test");
Trace.Unindent( );
}
}
public class TextBoxWriter : System.IO.TextW riter
{
TextBox _textBox;
public TextBoxWriter(S ystem.Windows.F orms.TextBox pTextBox)
{
_textBox = pTextBox;
}
public override void WriteLine(strin g value)
{
object[] x = {value};
if(_textBox.Inv okeRequired)
{
_textBox.Invoke (new UpdateTextDeleg ate(UpdateText) ,new object[]
{value});
}
else
{
_textBox.Text += value + "\r\n";
}
}
public override System.Text.Enc oding Encoding
{
get
{
// TODO: Add TextBoxWriter.E ncoding getter implementation
return null;
}
}
private void UpdateText(stri ng pString)
{
_textBox.Text += pString + "\r\n";
}
}
}