473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Enterprise Library Trace to TextBox

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?

Mar 15 '07 #1
2 4306
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";
}
}
}

Mar 15 '07 #2
On 15 Mar, 15:58, "DeveloperX" <nntp...@operamail.comwrote:
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";
}
}

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

Mar 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Dalberg | last post by:
I am getting the error below many times when I am trying to use the Enterprise Library. The solution seems to be a reboot. I am using Windows 2003 as my dev box. The error also happens with other...
1
by: David Herbst | last post by:
Enterprise Library Jan 2006 with Visual Studio 2005 on Windows 2000 Server sp4. My custom exception formatter fails with a "Unable to handle exception: 'LoggingExceptionHandler'." exception. ...
2
by: zahaby | last post by:
Is it applicable to use the Microsoft Enterprise Library in Logging without using the configuration tool ? I would like to configure and choose the trace listener programmatically . Thanks in...
0
by: Eniac | last post by:
Hello, I've started using Enterprise Library 2.0 recently and I've encountered a problem that seems to be ... well... undocumented :) Basically, when I set a Trace Listener (formatted event...
7
by: galico | last post by:
Hi All, We are having a very strange problem with the above. We have designed an application in ASP.NET 2.0 that uses the enterprise library data application blocks amongst others. We seem to be...
1
by: Jess Chadwick | last post by:
I am attempting to use the Enterprise Library (Jan 2006) Cryptography block to encrypt a credit card number in my ASP.NET 2.0 Commerce Server application. Everything is configured correctly, as...
1
by: sreedharv | last post by:
Log4Net vs Enterprise Library - Logging Application Block Please list or point to the comparison. My requirements are: - 50 to 70 web servers - Track Machine name - Extract class / method...
0
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Hi, Thanks in advance for reading this. Not sure where to post this question, but I hope someone in here can help. Trying to write to Event Log in VS 2005 (.NET 2.0) using Enterprise Library...
0
by: =?Utf-8?B?UG9sbHkgQW5uYQ==?= | last post by:
Hi, I have previously used EL v 3.1 Exception Handling application block successfully. I thought I would now try to do the same with EL v 4.0. My first experiment was to replace an exception....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.