Logging Application Block

First, we need to know what application blocks are. Application Blocks are reusable software components and are part of Microsoft's Enterprise Library. The enterprise library provides configurable features to manage crosscutting concerns like validation, logging, exception management, etc. Here I elaborate on one of the features of the Enterprise Library- Logging Application Block. One can use the Logging Application Block to log information to:
  1. Event logs
  2. A text file
  3. An email message
  4. A database
  5. A message queue
  6. Windows Management Instrumentation (WMI) event
  7. Customize the logging location
Create windows form application. Now add a reference to Enterprise Library Logging Application Block to the solution.
 
Logging Application block
 
Now add a configuration file for configuring the log. Now edit it with the  Enterprise Library Configuration.
 
Logging Application
 
It opens a window. In that select Blocks -> Add Logging Settings
 
Application block
 
It creates default logging settings as follows:
 
Logging Application
 
It contains a category named "General", which is using an "Event Log Listener" with a "Text Formatter". For now, we can leave Special Categories. So, what all these are doing?
 
Let's understand it with an example. There may be some logs which you need to write in an event log and a text file, and for some log you need to mail also. So you create one category which writes all logs to the event log and text file, and another category for sending mail of only error logs. Then you have to create 3 listeners, for logging in event log, text file, and mailing. You can also define different formatters for different types of listeners. For example, you need a detailed log for logging in events log and precise log for mailing. All these settings are defined in the configuration. All you need to do is when you write a log just defines the category of the log and the rest is done by the application block.
 
Categories:
 
Category can filter the log entry and route it to different listeners. The filter can be based on the severity, i.e., Critical, Error, Warning, or Information.
 
Logging Target Listeners:
 
Different listeners can be defined here. We only need to do configuration and these listeners can write to event logs, a flat file, xml file, to the database, send mail etc.
 
Log Message formatters:
 
This is used to define a format in which the log is written. Text formatter is the default formatter, which writes all the information. For customizing the formatter, define a template.
 
A sample project was uploaded. This contains a general category that logs to event logs and a flat file using text formatter. And mail category which sends mail using mail formatter. And depending on the conditions it logs to general category, or sends mail, or can do both.
 
So now you can see a configuration section called "loggingConfiguration" in the app.config file.
 
This configuration section contains listeners, formatters, categorysources and specialSources. One can configure listener, formatter, categories here.
 
To write a log you just need to create a LogEntry and specify the category. Of category is not specified, log will be written to default category.
 
So, now you can see logging is so easy using Application blocks.