Articles‎ > ‎

[ Article ] How to configure and use Microsoft Enterprise Library 5 for logging


This post should help you in solving common questions like 

How to log exception using Microsoft Enterprise Library 5.0.

How to configure Microsoft Enterprise Library to log exception.

How to configure Microsoft Enterprise Library to log exception to rolling flat file.

How to configure and use Microsoft Enterprise Library 5.0 to log exception

Before getting into details lets have a short introduction to Enterprise Library.

Microsoft Enterprise Library 

Microsoft Enterprise Library is a collection of reusable library designed to help developers with common enterprise development challenges. It is a set of libraries for the Microsoft .NET Framework. It provides API to facilitate common practices like data access, security, logging, exception handling and others. Enterprise Library is provided as pluggable binaries and source code, which we can freely use and customized for own purposes.

Benefits of Microsoft Enterprise Library 

  • Improved productivity: Each of the Application Blocks provides several interfaces meant to satisfy common application concerns.
  • Configuration-driven design: Many technical decisions about the application behavior can be delayed until configuration time of the application. For instance, if an application does not properly handle an exception, instead of adding another exception handler to the code, an administrator can configure an additional exception handling policy.
  • Improved testability: Many application areas can be reconfigured to improve testing of the application in isolation.
  • It reduces the work load of the developer

Microsoft Enterprise Library Application Blocks

Each application block in Microsoft Enterprise Library addresses a specific concern; This Application Blocks may be used equally in a web, smart client or service-oriented application. The patterns and practices team also produces more specialized Application Blocks that are not included in Enterprise Library, such as the User Interface Process Application Block, Aggregator Application Block, Updater Application Block and the Composite UI Application Block.

How to install Microsoft Enterprise Library 5.0

You can download Microsoft Enterprise Library 5.0 from http://www.microsoft.com/en-us/download/details.aspx?id=15104. After downloading, install the Enterprise Library 5.0 as administrator. After installing, it is accessible just like any other installed programs. 
Enterprise Library 5.0 comes with a configuration editor with which we can create and edit application configuration.

How to use Enterprise Library 5.0 configuration editor tool

 Let’s open the Enterprise Library 5.0 configuration editor tool. For this, Go to Start-> All Programs -> Microsoft Patterns and Practices -> Enterprise Library 5.0 -> Enterprise Library Configuration and select the appropriate tool. The system which I am using is a 64 bit OS and I am using framework 4.0. So I will select “EntLib Config .NET 4.0”

How to use enterprise library 5.0 configuration tool

How to configure Application Block using Microsoft Enterprise Library Configuration Tool

Now we are going to create a new config file with the help of Enterprise Library 5.0 configuration editor. This config file will be saved by this tool to a specified location. To create this, click on

File-> new

After you have clicked on this, you will get a much like a blank window. Now we are going to add a Logging Settings to the configuration file. For this, click on

Blocks->Add Logging Settings

When you click on this, this will add some default logging settings. Now we have to configure it for our own purpose. The default logging option which the configuration tool provides is an Event Log Listener. What we are going to use here is a Rolling Flat File Listener. Let’s see which all options we have.

How to use enterprise library 5.0 configuration tool


logging trace listeners

  • Custom trace listener -  A logging target listener  which adds log to a custom log listener
  • Database trace listener – A logging target listener that sends log message to a specified database
  • Email trace listener - A logging target listener which sends log message to a specified email server.
  • Event Log Trace Listener - A logging target listener which sends log message to a specified windows event log
  • Flat File Trace Listener - A logging target listener which writes log message to a specified text file
  • Message Queuing Trace Listener - A logging target listener which writes log message to a specified windows queuing queue.
  • Rolling Flat File Trace Listener - A logging target listener which writes log message to a specified text file. This can roll the log file in specified interval.
  • System Diagnostics Trace Listener – Abstract base classes for trace listeners which monitor trace and debug output.
  • WMI trace listener – A logging target listener which raises a WMI event for each log message.
  • XML trace listener – A logging target listener that logs message in xml format to a specified text file.

How to add Logging Trace Listeners With Enterprise Library 5.0 Configuration Tool

To add Logging Trace Listeners With Enterprise Library 5.0 Configuration Tool Click on the add button (+) for Logging Target Listeners and Select rolling flat file logging out of it.

How to configure enterprise library 5.0 to use rolling flat file trace or log listener


now after selecting the rolling flat file option, we have to configure it.

How to configure enterprise library 5.0 to use rolling flat file trace or log listener

Change the “File Exists” to Increment which is to roll the log file when it reaches a particular size or the interval has reached. When this option is selected old log files will be retained and new logfile will be created with a timestamp attached to file name.

Change “Formatter” to “Text Formatter” There will be a default log message formatter which is available when we add logging block, This will be available in Formatter drop down.

Change “Roll Interval” to Day it will create a new log file every day.

Change “Roll Size KB” this configuration will roll the log file if it reaches the max size specified here. We will select 1000 here.

Keep the rest of the option as same. After this we have to change the Logging settings. For this go to “Categories” section in “Logging settings”.

How to configure enterprise library 5.0 to use rolling flat file trace or log listener


 Click on the Listeners dropdown and select the newly configured Rolling Flat File Trace Listener which will be available in the drop down once we configured Logging and Target Listeners. Change the Listeners dropdown of “Logging Errors & warnings” section to Rolling flat file.

How to configure enterprise library 5.0 to use rolling flat file trace or log listener

 By default it will be logging to the default option which was Event Log listener. Here we are not at all using event log listener so we are changing it so that errors and warnings will be logged on to rolling flat file. Now delete event log listener, because we are not using it and if we keep it there, then we will get unnecessary line of code in our config file.

How to configure rolling flat file trace listener using enterprise library 5.0 configuration tool.

Now click on 

file -> save as

And give one config file name and click save. This will save the settings which we have configured using Enterprise Library Configuration 5.0 tool in a config file which you have specified.

How to use Enterprise Framework Logging Block in code

Now go to visual studio and create a new project. Right click on the project in visual studio and click on add new item.  Add application configuration file. Now copy the configuration tag content from the file which we have created with enterprise library configuration tool and paste it in the configuration tag of the application configuration file. Now in the project , to the class where you need logging, add reference to

System.Diagnostics;

Microsoft.Practices.EnterpriseLibrary.Logging;

and try the code as shown below.

LogEntry logEntry = new LogEntry {Message = "Error Message", Severity = TraceEventType.Error}; 

Logger.Write(logEntry);


Now when this line of code is executed, you will get an entry in your log file as

----------------------------------------

Timestamp: 7/16/2012 1:00:58 PM

Message: Error Message 

Category: General 

Priority: -1 

EventId: 0 

Severity: Error 

Title: 

Machine: 2071259 

App Domain: WindowsFormsApplication1.vshost.exe 

ProcessId: 12912 

Process Name: //your process name(Original name is hidden)

Thread Name: 

Win32 ThreadId:13772 

Extended Properties:

----------------------------------------

You can configure the logEntry object to change the default options.

Configure Microsoft Enterprise Library 5.0 to log exception to database.

Now lets see how to use database to store the log entries. Logging to database is a little tricky. You need some tables and stored procedures to have it logged into database. Enterprise Library comes with a script which when runs against an SQL Server database, will create all those necessary tables and stored procedures. Let’s see how this is done.
Go to the directory “EntLib50Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts”. You can find this directory where you have installed Enterprise Library Source files. If you could not find this directory, then it may be because you have not installed the source. To install this, go to the directory where you have installed Enterprise Library 5.0 and find this directory “Microsoft Enterprise Library 5.0\src” there you can find the install file which will be named as “Enterprise Library 5.0 - Source Code.msi” install that. After installing, you will be able to find the scripts directory.
Now go to the scripts directory and find the command file “CreateLoggingDb.cmd” run this and it will create a database called “Logging”. The command file will execute the SQL script in “LoggingDatabase.sql” file which you can find in the same directory as the command file. If you need to have the database name as something else, or if you want the log entries to be logged into the some database which you are already using for your application, then you have to edit the script file to change its database name and then run the script.

After doing this, Go to the configuration editor. And configure the logging settings. In configuration editor, Go to blocks and select “Add Logging Settings”. Now we have to add a "Logging Target Listener". Click on the (+) on "Logging Target Listener" and select Database Trace Listener as shown in the image below.

Adding database backing store

Now we have to configure the Database Settings. To the Database Settings, Database Instance, change the database dropdown of the connection string settings to System.Data.SqlClient. Now change the connection string setting to Database=Logging;Server=local)\SQLEXPRESS;Integrated Security=SSPI.

Adding sql client as database

Now to the Logging settings, change the Listeners of general section to “Database Trace Listener”

Select database trace listener

Now save this settings to your application config file. Now add a reference to Microsoft.Practices.EnterpriseLibrary.Caching.Database in your project. Now use the code which is used in the above section to log your log entry. Run your application and find the log entry in your database.

Hope this helps. :)

Enterprise library application block can be installed to your project using NuGet Package manager. To see how, have a look at How to configure and use Enterprise Library 5.0 application blocks using NuGet Package Manager

Comments