Sunday, May 25, 2008

Multi tier Architecture utilities (NLOG)

see these Related topics:

  1. Multi tier basics
  2. A best practice for multi tier architecture
  3. A sample code based on best practice
  4. Examine utilities to speed up Multi tier programming- NLog

Please download the second version of Multi tier code

In previous lessons I talked about basic concepts of Multi tier and a best practice Architecture. Also I examine a sample code about that Architecture in this lesson I am going to review ”NLOG” one of the best tools that with no doubt you better use in that architecture to Log both errors and interaction with application.
What is NLOG?
NLOG is a free tool could be used to log into different resources like file, event viewer, database, Memory, Console, MSMQ, Mail and Web Service and etc. Meanwhile it can be configured to different level of log. Moreover, it can be switched off or on. Also you can have different source of log. Above of all it is open source application. Should you need any changes you will be able to make it.
How NLOG works?NLog gives you a class library to call its services. Also it has a configuration file called NLog.config where you set which log sources you need for instance file or database. Also you can configure the source for example for a file you probably configures the file name. You can have multiple sources. Take look at this sample:


this configuration file will be in UI application then other layers in Multi tier architecture do not need extra Nlog.config file all of them will use this configuration file. If you look at the logDirecotry in above code you will see that actually nlog will create a folder called Logs inside application folder and it will log data for each day. I just showed you logging in files. Log in other data sources like database has its own configuration variables.
It you look at the rules tag you will find two logger tags where you define each one of these logger
1- which class will represents them (“name”)
2- what is the minimum level of log also you can disable log by setting this value to debug for a release version of application (“minLevel”)
3- Which target this logger is using to log information

In a multi tier application we always put our logger in Common Layer because we need this library in other layers. Also I highly recommend you to write a rapper class to simplify using NLog. Take a look at the DataLogger code:



In above code simply two methods are written Info and Warn to log information. If you look at the parameters you see multiple parameters. To save these parameters an xml structure is being used.
Please see the next version of multi tier code with logging
I remind you that I used very simple logging here you may change it and adapt it to your requirements. So this is not a best practice however, it is a good practice using Nlog in your projects. Nlog is really a lot more than I show you here. If you need any extra information or you think it is worth to have more detail information about NLOG let me know and raise some comments here. Do not underestimate this fabulous tool. I believe it needs at least 20 posts to show its capability!
In next lesson I am going to talk about my favourite tool Code Smith to generate code. Please download the next version of the code.

4 comments:

Nahid said...
This comment has been removed by the author.
Nahid said...

Dear Emad,
Thank you for being such a great teacher. I really enjoy being here. The skills and knowledge I gain will surely help me in my career. I'm most eager for your posts about Multi Tier and distributed Architecture and their utilities. I'll be so happy if you write more. I want to know if its possible to introduce me some more resources to study based on your experiments there, hopefully I'll be there next year and I like to get more ready and I'm needy for your advice.
Thanks again for your wonderful teaching.

Anonymous said...

Dear Emad i surprised and glad when see you there! I'm Ehsan's friend (Shalchian) and heard about you when he was in Iran. First tanks for teaching and I hope you to enjoy your life.
I want to use The NLOG in our project but i couldn't find it free! and if it's possible for you please talk more about enterprise software Architecure.

Thanks again

Anonymous said...

Dear Emad i surprised and glad when see you there! I'm Ehsan's friend (Shalchian) and heard about you when he was in Iran. First tanks for sharing your experience and I hope you to continue your teching not like to Ehsan ;)
I want to use The NLOG in our project but i couldn't find it free! and if it's possible for you please talk more about enterprise software Architecure.
Thanks again