473,414 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Asynchronous logging using delegates

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
Nov 17 '05 #1
3 4485
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]
- mv*@spam.guard.caspershouse.com

"Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #2
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #3
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Oscar Thornell" <oscar.thornell [ xx] gmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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



Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ollie Riches | last post by:
I have managed to get asycnhronous logging work for an asp.net application and I wanted some clarification on the method. Normally if the application is logging synchronously it has all the...
4
by: theBoringCoder | last post by:
Hello All, I just started learning about Delegates, and was wondering if someone could point me at some useful, straightforward examples of how to use delegates to make method calls...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
1
by: Ollie Riches | last post by:
I am trying to use asynchronous logging using MSMQ and I have configured the properties as defined in the help (see below) I don't understand where the property 'MsmqPath' is in the app.config for...
3
by: dvy | last post by:
I know it is supported in C# and VB,In VC so that??
2
by: Gerda | last post by:
Hi! I've implemented many times an asynchronous call of a method with a call backfunction successfully. But to implement this with VB.NET is not so successfully. I can implement all events...
0
by: r1 | last post by:
I am relatively inexperienced in using delegates and asynchronous methods. I read several articles, and then I developed my own code with a mission to improve the performance. Wow! I cannot...
4
by: 6954 | last post by:
Hi! i need to implement some asynchronous call to my com+ component, but i need it to return some values (e.g. results of sql select statement). obviously queued components and MSMQ are out of...
3
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.