Writing output of custom trace listener to other listeners, possible?

Topics: Building and extending application blocks, Logging Application Block
Mar 14, 2011 at 6:34 PM

Hi! Is it possible to create a custom location(custom trace listener) that would send a LogEntry further so all configured trace listeners can react on that? 

I know it's unusual, but basically we want in a custom trace listener to write to a memory object as opposed to a database/file/etc. Then the memory object whatever it is (queue/arraylist) should trigger an event that would span a background thread that should send the LogEntry object further, so all configured trace listeners can pick up and process the entry. 

Is it possible at all? I suppose I can call again to WriteLog, but how can I distinguish? 

 

Many thanks in advance,

 

Mar 15, 2011 at 1:52 AM
Edited Mar 15, 2011 at 1:53 AM

You mean how do you write to other trace listeners in a custom trace listener?  My suggestion would be to create a category in your logging configuration which would reference all the trace listeners you want to use within your custom trace listener.   You would pass the name of that category then to your custom trace listener via adding it to the Attributes property.  Do this using the config tool to make adding of values to the Attributes property easier.  You would access then that value in the custom trace listener in the .Attributes property collection using the key you entered in the config editor as the indexer.  You now have the logging category which you can make use to specify which category to log to.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 15, 2011 at 6:36 PM

Thank you, Sarah. Your answer brings me closer to what I need done.

 "You would pass the name of that category then to your custom trace listener via adding it to the Attributes property" - could you please point me where I can read up on this Attribute property, please? Perhaps you have a sample laying around somewhere?

Here's my listener:

namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class MyQueueTraceListener : CustomTraceListener
    {
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.WriteLine(data.ToString());
            }
        }
        public override void Write(string message)
        {
            Debug.WriteLine("Write " + message);
        }
        public override void WriteLine(string message)
        {
            //Here I am going to write to a queue and then have a background thread read from the queue
            // and propagate the message to other listeners
            Debug.WriteLine("WriteLine " + message);
        }
    }
}

Infinite thanks in advance,

 

 

Mar 15, 2011 at 6:37 PM

Thank you, Sarah. Your answer brings me closer to what I need done.

 "You would pass the name of that category then to your custom trace listener via adding it to the Attributes property" - could you please point me where I can read up on this Attribute property, please? Perhaps you have a sample laying around somewhere?

Here's my listener:

namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class MyQueueTraceListener : CustomTraceListener
    {
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (data is LogEntry && this.Formatter != null)
            {
                this.WriteLine(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.WriteLine(data.ToString());
            }
        }
        public override void Write(string message)
        {
            Debug.WriteLine("Write " + message);
        }
        public override void WriteLine(string message)
        {
            //Here I am going to write to a queue and then have a background thread read from the queue
            // and propagate the message to other listeners
            Debug.WriteLine("WriteLine " + message);
        }
    }
}

Infinite thanks in advance,

 

 

Mar 16, 2011 at 12:14 AM

You can access it using this.Attributes[keyName] in any of your custom trace listener's method.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 18, 2011 at 10:45 PM

I sent an email to entlib.support@avanade.com attention Sarah Urmeneta

Mar 21, 2011 at 7:52 AM

I already replied to the email.  If you need further help on it, just post it here in the Discussion forum.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com