PC Review


Reply
Thread Tools Rate Thread

Asynchronous logging using delegates

 
 
Oscar Thornell
Guest
Posts: n/a
 
      16th Sep 2005
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


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      16th Sep 2005
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 Removed)

"Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
news:%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Oscar Thornell
Guest
Posts: n/a
 
      16th Sep 2005
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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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 Removed)
>
> "Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
> news:%(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?VGVycnlS?=
Guest
Posts: n/a
 
      24th Sep 2005
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" wrote:

> 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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
> message news:%(E-Mail Removed)...
> > 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 Removed)
> >
> > "Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
> > news:%(E-Mail Removed)...
> >> 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
> >>

> >
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous Invoke and the UI thread (using delegates) =?Utf-8?B?YnJpc2Vycw==?= Microsoft VB .NET 7 6th Mar 2006 06:45 PM
Asynchronous Delegates Tobias Matzat Microsoft C# .NET 1 14th Oct 2005 09:26 PM
Delegates & Asynchronous Callback standogan@gmail.com Microsoft C# .NET 2 11th Aug 2005 05:49 PM
Asynchronous delegates is supported in C++/CLI? =?Utf-8?B?ZHZ5?= Microsoft VC .NET 3 7th Aug 2005 09:38 AM
Asynchronous Methods and Delegates theBoringCoder Microsoft C# .NET 4 10th May 2004 10:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.