|
-
May 12th, 2008, 11:45 PM
#6
Re: Trace messages to a control based on filtering..how???
Yes the point is Override the write method...
Do already done this???
Here my code for write a trace to the list box.
Code:
internal class MinTraceListener : TraceListener
{
private const int MaxEntries = 10000;
private const float BufferEntriesPercent = 0.1f;
private ListBox _outputList;
/// <summary>
/// Constructor
/// </summary>
/// <param name="outputList">The list box to output the trace events</param>
public MinTraceListener(ListBox outputList)
: base()
{
_outputList = outputList;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="outputList">The list box to output the trace events</param>
/// <param name="name">Name of the listener</param>
public MinTraceListener(ListBox outputList, string name)
: base( name )
{
_outputList = outputList;
}
/// <summary>
/// Gets a value indicating whether the trace listener is thread safe.
/// </summary>
/// <value></value>
/// <returns>true if the trace listener is thread safe; otherwise, false. The default is false.</returns>
public override bool IsThreadSafe
{
get
{
return false; //force Trace.UseGlobalLock
}
}
/// <summary>
/// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
/// </summary>
/// <param name="message">A message to write.</param>
public override void Write(string message)
{
if (message == null)
return;
if (Dispatcher.CurrentDispatcher != _outputList.Dispatcher)
{
_outputList.Dispatcher.Invoke( DispatcherPriority.Render, (System.Windows.Forms.MethodInvoker)delegate { Write( message ); } );
return;
}
string[] items = message.Split( '\n' );
foreach (string item in items)
{
ListBoxItem i = new ListBoxItem();
i.Content = item;
_outputList.Items.Add( i );
}
int bufferEntries = (int)(Math.Ceiling( MaxEntries * BufferEntriesPercent ));
if (_outputList.Items.Count > MaxEntries + bufferEntries)
{
for (int i = 0; i < bufferEntries; i++)
{
_outputList.Items.RemoveAt( i );
}
}
}
/// <summary>
/// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
/// </summary>
/// <param name="message">A message to write.</param>
public override void WriteLine(string message)
{
Write( message );
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|