Articles‎ > ‎

[ Article ] How to configure and use Enterprise Library 5.0 application blocks using NuGet Package Manager


Visual studio supports many features so that we can easily add packages to our project. NuGet package manager is a collection of tools to automate the process of downloading, installing, upgrading, configuring, and removing packages from a VS Project. To Add packages to a project you will need a NuGet package manager.

To see how to install NuGet package manager see the post on it How to install NuGet package manager for visual studio 2010After installing NuGet Package manager, to the project where you need the logging library block, right click on the project and select "Manage NuGet Packages"

How to install enterprise library via NuGet package manager


Now on the wizard which opens up, you can see a search box. Enter "Enterprise library"

how to install enterprise library via NuGet package manager

Now click on the install! thats it. It will install and add reference to all the dll required to the project.

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. 

How to use enterprise library 5.0 configuration tool

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.

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 drop-down 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

Microsoft.Practices.EnterpriseLibrary.Logging;

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.

Hope this helps. :)


Comments