C# Enterprise Library Asynchronous Logging

We all know how slow any logging mechanism is, specially when one of the trace listeners is logging to a database. By using .NET 4.0 with it’s Parallel Library (System.Threading.Task) we can easily turn our logger to use fire-forget asynchronous operation.

 Example below uses Enterprise Library 5.0.

Suppose this is your existing LogMessage method:

Just add another method LogMessageAsync with ff code:

Now to log asynchronously just call the LogMessageAsync() method.

 

 

C# Parallel – Call methods with input and return values

In .NET 4.0 they made it easy to start a task/process simultaneously. Code below shows how you can execute two methods with input and return values simultaneously thru the Tasks Factory in System.Threading.Task namespace.

Note: Add System.Threading.Tasks & System.Diagnostics in the using statement.

Code:

 

 

Output:

>PLINQ – Pallalel Computing Made Easy

>static void Main(string[] args)

{
List myCollection = new List
{
“Item1”,
“Item2”,
“Item3”,
“Item4”,
“Item5”,
};


myCollection
.AsParallel()
.ForAll(item =>
{
string newItemName = string.Format(“Formatted Item:{0}_{1}”, item, System.Guid.NewGuid());
Console.WriteLine(newItemName);
}
);

Not really an intensive processing right, but atleast you get the idea.

}