Asynchronous logging using delegates

  • Thread starter Thread starter Oscar Thornell
  • Start date Start date
O

Oscar Thornell

Hi,

I am thinking about doing all my logging asynchronously using a delegate.
The main resaon for this would be performance and responsiveness of the
application (an ASP.NET app).

//Exampel
delegate void LogDelegate(string message);
LogDelegate logger = new LogDelegate(Log.Debug);
logger.DynamicInvoke("Some message..");

I appreciate feedback about the approach/idea! Like the overhead of creating
a delegate and so on...

Regards
/Oscar
 
Oscar,

Unfortunately, calling DynamicInvoke will not cause the delegate to be
invoked asynchronously. It only causes the delegate to be invoked on the
calling thread.

You need to call the BeginInvoke method on the delegate to be called.

Also, you should wrap this up in a utility method.

There is one problem. If you make a call to BeginInvoke, you are going
to end up taking threads from the thread pool to process your writes to the
log. Your pages are processed on this same thread pool. If you have a good
number of writes, you are going to impact the performance of your app.

Finally, make sure that you create a delegate to be called when the
asynchronous call is completed. Calling a delegate asynchronously leaks a
ManualResetEvent which you will need to close down manually. In the event
handler, call Dispose on the WaitHandle exposed by AsyncWaitHandle on the
IAsyncResult passed into the method. You could let the GC handle them (and
they will be cleaned up by a GC eventually), and it might even be viable in
an ASP.NET application, since GC's occur on a more predictable schedule
(assuming traffic patterns are steady).

Hope this helps.
 
You´re right I should call BeginInvoke and then later EndInvoke on the
delegate to avoid leaks..my mistake with dynamicinvoke..
Everything probably wrapped up in a utility class...but that was not rely
the question here...

I was looking for pros/cons using asynchron behaviour for a logging
mechanism...
Is it worth it?

/Oscar

Nicholas Paldino said:
Oscar,

Unfortunately, calling DynamicInvoke will not cause the delegate to be
invoked asynchronously. It only causes the delegate to be invoked on the
calling thread.

You need to call the BeginInvoke method on the delegate to be called.

Also, you should wrap this up in a utility method.

There is one problem. If you make a call to BeginInvoke, you are going
to end up taking threads from the thread pool to process your writes to
the log. Your pages are processed on this same thread pool. If you have
a good number of writes, you are going to impact the performance of your
app.

Finally, make sure that you create a delegate to be called when the
asynchronous call is completed. Calling a delegate asynchronously leaks a
ManualResetEvent which you will need to close down manually. In the event
handler, call Dispose on the WaitHandle exposed by AsyncWaitHandle on the
IAsyncResult passed into the method. You could let the GC handle them
(and they will be cleaned up by a GC eventually), and it might even be
viable in an ASP.NET application, since GC's occur on a more predictable
schedule (assuming traffic patterns are steady).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oscar Thornell said:
Hi,

I am thinking about doing all my logging asynchronously using a delegate.
The main resaon for this would be performance and responsiveness of the
application (an ASP.NET app).

//Exampel
delegate void LogDelegate(string message);
LogDelegate logger = new LogDelegate(Log.Debug);
logger.DynamicInvoke("Some message..");

I appreciate feedback about the approach/idea! Like the overhead of
creating a delegate and so on...

Regards
/Oscar
 
Theres is substaintail overhead involved with making a delegate call, mostly
likely this
won't be a problem, and your approach is sound.

If performance is a major concern, you can have your main worker thread
write to a shared cirucular queue of trace messages, and have a didicated
logging thread, display these messages. Until the trace queue fills up,
theres almost no overhead for the worker thread. When the trace queue is
filled, the worker thread will be block until the logger thread catches up.

You can also take a look at log4net, a tracing assembly. Its not as feature
rich as Microsoft's tracing, and exception applicaiton block, but its more
performant.

Oscar Thornell said:
You´re right I should call BeginInvoke and then later EndInvoke on the
delegate to avoid leaks..my mistake with dynamicinvoke..
Everything probably wrapped up in a utility class...but that was not rely
the question here...

I was looking for pros/cons using asynchron behaviour for a logging
mechanism...
Is it worth it?

/Oscar

Nicholas Paldino said:
Oscar,

Unfortunately, calling DynamicInvoke will not cause the delegate to be
invoked asynchronously. It only causes the delegate to be invoked on the
calling thread.

You need to call the BeginInvoke method on the delegate to be called.

Also, you should wrap this up in a utility method.

There is one problem. If you make a call to BeginInvoke, you are going
to end up taking threads from the thread pool to process your writes to
the log. Your pages are processed on this same thread pool. If you have
a good number of writes, you are going to impact the performance of your
app.

Finally, make sure that you create a delegate to be called when the
asynchronous call is completed. Calling a delegate asynchronously leaks a
ManualResetEvent which you will need to close down manually. In the event
handler, call Dispose on the WaitHandle exposed by AsyncWaitHandle on the
IAsyncResult passed into the method. You could let the GC handle them
(and they will be cleaned up by a GC eventually), and it might even be
viable in an ASP.NET application, since GC's occur on a more predictable
schedule (assuming traffic patterns are steady).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oscar Thornell said:
Hi,

I am thinking about doing all my logging asynchronously using a delegate.
The main resaon for this would be performance and responsiveness of the
application (an ASP.NET app).

//Exampel
delegate void LogDelegate(string message);
LogDelegate logger = new LogDelegate(Log.Debug);
logger.DynamicInvoke("Some message..");

I appreciate feedback about the approach/idea! Like the overhead of
creating a delegate and so on...

Regards
/Oscar
 
Back
Top