473,586 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(str ing message);
LogDelegate logger = new LogDelegate(Log .Debug);
logger.DynamicI nvoke("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 4501
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
ManualResetEven t 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.co m

"Oscar Thornell" <oscar.thorne ll [ xx] gmail.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.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(str ing message);
LogDelegate logger = new LogDelegate(Log .Debug);
logger.DynamicI nvoke("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.c om> wrote in
message news:%2******** ********@tk2msf tngp13.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
ManualResetEven t 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.co m

"Oscar Thornell" <oscar.thorne ll [ xx] gmail.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.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(str ing message);
LogDelegate logger = new LogDelegate(Log .Debug);
logger.DynamicI nvoke("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.c om> wrote in
message news:%2******** ********@tk2msf tngp13.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
ManualResetEven t 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.co m

"Oscar Thornell" <oscar.thorne ll [ xx] gmail.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.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(str ing message);
LogDelegate logger = new LogDelegate(Log .Debug);
logger.DynamicI nvoke("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
1525
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 settings for the logging application block in the app.config for the application. But if you want to use asynchronous logging you have to specify...
4
4214
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 asynchronously. I've been told that this is possible, and works similar to callbacks in C++, but cannot find any straightforward examples in C#. Thanks,
1
2833
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 remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because...
1
8184
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 MSMQDistrbutor.exe is it meant to be an aplication settings or an enterprise library setting in the app.config? I have tried creating an...
3
1597
by: dvy | last post by:
I know it is supported in C# and VB,In VC so that??
2
5356
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 ProcessingEvent, CompletedEvent, etc. It works fine and copies all files in the background ... Bit to make it perfekt I want to implement a callback...
0
1268
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 believe the difference in speed! However, the asynch operation fails sometimes, despite of the fact that it works most of the time. I am really at a...
4
2194
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 the question... anyone has any ideas how to implement it? or just a guideline maybe? thank you
3
2086
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 different stages of execution. However I was wondering if there was any information available regarding the limitations of the asynchronous model, in...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.