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.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";
}
}
}