Friday, 19 January 2018

Logging With Log4Net | Console App | Parallel Vs Delegate


In this article, we will see how to enable logging using log4net and see the advantage of using Parallel Invoke over delegate.
Here is the simple console and I added the reference of Log4net; using log4net api I added tracing statements as shown.
I have used delegate to invoke LogM1 and LogM2 methods that would in turn invoke M1 and M2 methods using Parallel For Loop, M1 and M2 writes the trace details as shown below.
Lets run and verify the logs. Though, we use delegate to invoke the methods, we clearly see, M2 is invoked or started; post completion of M1 method. As Parallel FOR loop invokes M1 and M2, logging are captured in a random manner.
Instead of delegate, lets now refactor the code to use Parallel Invoke method as shown below.
Lets run and verify the behaviour. Both M1 and M2 are parallel invoked and all the logs are captured in a random manner; execution time is also faster comparatively to earlier way using delegate.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.