How can you implement Buffered Logging?

Topics: Logging Application Block
Oct 1, 2009 at 10:38 PM

Hello,

I created a CustomTraceListener based on http://entlib.codeplex.com/Thread/View.aspx?ThreadId=51701  .

It works fine but I was wondering if there is a way to buffer the log messages and write them all at once when athey reach a count?

Thank you,

Burak

 

Oct 2, 2009 at 4:53 AM
Edited Oct 2, 2009 at 5:52 AM

Hi,

Here is my idea of buffering the log:

Store the LogEntries to a collection type variable inside the custom tracelistener while the max count is not yet reached. else start logging.

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class BufferedLogger : CustomTraceListener 
    {
        private ArrayList table = new ArrayList(20);

        public override void Write(string message)
        {

        }

        public override void WriteLine(string message)
        {
        }

        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {

            if (table.Count != 20)
            {
                table.Add(data);
            }
            else
            {
                foreach (var item in table)
                {
                    if (this.Formatter != null)
                    {
                        string formattedEntry = this.Formatter.Format(data as LogEntry);
                        Debug.Write(formattedEntry);
                    }
                    else
                    {
                        Debug.Write(data.ToString());
                    }
                }
                table.Clear();
                table.Add(data);
            }
        }
    }

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 2, 2009 at 12:47 PM

Thanks for this suggestion.

What if the buffer size doesn't reach the preset number (20)? How will this get flushed then?

Isn't there a buffering option for logging application block similar to healthmonitoring?

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

healthMonitoring heartbeatInterval="10000" enabled="true">

   <bufferModes>

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

     <

</font></font><font size="2" color="#0000ff">

 

</font>

clear/>

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

    <

</font></font><font size="2" color="#0000ff">

 

</font>

add name="Logging" maxBufferSize="1000" maxFlushSize="200" urgentFlushThreshold="800" regularFlushInterval="00:30:00" urgentFlushInterval="00:05:00" maxBufferThreads="1"/>

  </bufferModes>

Thank you,

Burak

Oct 2, 2009 at 3:43 PM

As far as I know, there is no buffering option in the logging application block. You would have to implement your own in order to achieve that behavior.

Related post: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=7233

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 1, 2009 at 1:33 PM

Hi,

By any chance did anyone work on this?

Even I'm supposed to implement this functionality.

I want to implement functionality similar to "BufferingForwardingAppender" from Log4Net.

I haven't started working on it yet. But I'm supposed to start soon.

I'm worried about only 2 points:

1> What if the buffer size doesn't reach the preset number (20)? How will this get flushed then? (This is from the earlier post.) Exactly same problem.

2> How do I call another tracelistener from my custom trace listener.

OR How do I send the buffered messages to the specified Trace Listener.

E.g. suppose user wants to send buffered messages to “Event Log Trace Listener”.

Later he changes it to “Flat File Trace Listener”. The user should be able to do it without any code changes.

 

It would be helpful any guidance/pointers are provided.

 

Regards,

Abhijeet.

Dec 1, 2009 at 11:55 PM

1> What if the buffer size doesn't reach the preset number (20)? How will this get flushed then?

        - You can set a timer as well which will also limit the time up to when the buffer should reach the maximum number. 

2> How do I call another tracelistener from my custom trace listener.

        - Answered in your other post - http://entlib.codeplex.com/Thread/View.aspx?ThreadId=76701

 

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