Trace messages to a control based on filtering..how???
Hi,
How do i set up two trace listeners which does tracing on the basis of some condition???
Scenario:
My application has a trace listener setup which outputs the trace messages into a log file.I need to write another trace listener which basically outputs specific traces into a control(RichtextBox).The application has traces sprinkled all over and all those traces are meant to go to the log file.Now i need to put traces based on some specific functionalities being performed.Those traces should be going to my Control.
For example:
All traces for a server roundtrip goes to the log file along with the request and response XML's
Traces like **Operation Started**,**Operation ended** should go to my control.
I have setup a seperate tracelistener whose write method outputs the trace to the control,but the issue is that whenever i say Trace.WriteLine("someTrace") both my traces are getting called.How do i filter this and direct specific traces to file and specific traces to control.
Thanks,
MMx
Re: Trace messages to a control based on filtering..how???
From what I understand, TraceListeners need to be registered to classes. If you have a class that needs to register both TraceListeners, then you may need to create 2 separate TraceListener classes, one for File writes and one for Control writes.
Unless someone here knows for sure, I personally don't think it's possible to have the same TraceListener class, which has 2 TraceListener objects registered to it, to only trigger one tracelistener in certain situations.
Re: Trace messages to a control based on filtering..how???
If you use the static method Trace.WriteLine(), you will write to all listeners in the Listeners collection. You can, however, write to a specific TraceListener.
Take the following example, in which 2 TraceListeners are created. When the instance methods are called, only the listener on which it was called writes the message. When the static method is called, both listeners pick it up.
PHP Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Trace.Listeners.Clear();
DefaultTraceListener dtl = new DefaultTraceListener();
Trace.Listeners.Add(dtl);
TextWriterTraceListener twtl = new TextWriterTraceListener(System.Console.Out);
Trace.Listeners.Add(twtl);
dtl.WriteLine("Testing DefaultTraceListener.");
twtl.WriteLine("Testing TextWriterTraceListener.");
Trace.WriteLine("Testing both.");
Console.Read();
}
}
}
Re: Trace messages to a control based on filtering..how???
Try to inherit from abstract class TraceListener and overwrite the write method...
Re: Trace messages to a control based on filtering..how???
Thanks for all your answers...
Now, creating seperate instances of 2 trace listeners wont work..as whenever any body says Trace.WriteLine("Something")...it gets written to both the traces..
I'm looking for some kind of filter...i see that we can apply filters for Listeners...mostly predefined ones...is there a way to override this beghaviour...so that i can set my own filter or something???
Thanks,
Mmx
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 );
}
}
Re: Trace messages to a control based on filtering..how???
Well...i see your point...but then the approach you mentioned is not standard tracing...as i see that the listener MinListener is not added to the listeners list of the application,and hence when you say Trace.WriteLine...it does't invoke minlistener.WriteLine...
But then it's equivalent to a normal class...which i could implement and call...also i thought tracing was happening on a background thread...in this case it will be on the main thread and hence impeding the regular flow.
Looking for more solutions..
Thanks,
Mmx
Re: Trace messages to a control based on filtering..how???
Quote:
Originally Posted by mmx_nexus
Well...i see your point...but then the approach you mentioned is not standard tracing...as i see that the listener MinListener is not added to the listeners list of the application,and hence when you say Trace.WriteLine...it does't invoke minlistener.WriteLine...
Yes we need to add this to Listener list. I can't post whole project :p. Here the way I put in the listener list
Code:
_traceListener = new MinTraceListener( _tracerList );
Trace.Listeners.Add( _traceListener );
for best practice you may use the config file... Is up to you...
Quote:
Originally Posted by mmx_nexus
But then it's equivalent to a normal class...which i could implement and call...also i thought tracing was happening on a background thread...in this case it will be on the main thread and hence impeding the regular flow.
well... when you want to write to the UI you must in the UI thread... Am I right?
Re: Trace messages to a control based on filtering..how???
The moment you add it to the listeners collection...all trace commands gets dumped into the listbox.
As i said if any part of the code says Trace.Writeline("message")...it gets dumped into the listbox,which is not what i want.