Tuesday, July 16, 2013

You are using common.logging ... right?

I thought everyone would be using common.logging right now for logging purposes, but just because I like it so much, I decided to write about it a bit.

Logging is of course a cross-cutting concern that you want to be able to do from everywhere within the layers of your application. Common.logging provides an abstraction between the library and the actual loggin implementation of the application. That's why you'll find that a lot of .NET libraries reference common.logging. This enables users of the library to hook up that common.logging to their own logging implementation in the actual application and see what the library is logging.

So I turned my invoice example into a nice n-tier application with a:
- Domain layer ( cross cutting )
- Data access layer
- Console application layer
- Utilities ( cross cutting )



This is Visual Studio 2012 'show on code map' - could be Ultimate only though ...

All of these projects are referencing common.logging. But only one project ( namely the 'presentation' layer  - console application in this case ) references common.logging.nlog ( an adapter for common.logging onto nlog ) and of course NLog itself:



Finally tweak your web.config or app.config for funneling all the common.logging stuff into the NLog targets you desire:



  
    
      
In every class you want to have a field like so:

private static readonly ILog log = LogManager.GetLogger();
Which allows you to access the log and get a nice prefix of what class is logging. I've decorated my 'GetRandomInvoice' method and the extension method with a few logging calls, and here's what you get:
10:07:31 ConsoleApp.Business.InvoiceService New random invoice is being created ...
10:07:31 ConsoleApp.Business.InvoiceService New random invoice creation is done!
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Computer' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Printers' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Paper' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Some more computer supplies' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Other stuff' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Product id 54' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Product id 64' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Product id 20' is being made to length 15
10:07:31 ConsoleApp.Utilities.StringExtensions String 'Total:' is being made to length 15
10:07:31 ConsoleApplication1.Program Generated invoice looks like this: 
Invoice - Id:1 Date:13-7-2013
----------------------------------
Computer         USD 899,00
Printers         USD 49,99
Paper            USD 12,50
Some more co...  USD 12,50
Other stuff      USD 12,50
Product id 54    USD 90,59
Product id 64    USD 70,13
Product id 20    USD 87,36
----------------------------------
Total:           USD 1231,00
As they say on .NET Rocks! Learn it, love it, use it.

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete