What is Logging?

Logging is nothing but the recording of processes, including initial parameters, to allow a detailed view into the activities of a system.

When to Use the Logging Application Block

If your applications have a requirement to log information to the event log, e-mail, a database, a message queue, Windows Management Instrumentation (WMI), or a file, you should strongly consider using the Logging Application Block to provide this functionality.
The Logging Application Block helps with application development in a number of ways:
  • It helps maintain consistent logging practices, both within an application and across the enterprise.
  • It eases the learning curve for developers by using a consistent architectural model.
  • It provides implementations that you can use to solve common application logging tasks.
  • It is extensible, supporting custom implementations of components that format and write event information.

Types of Logging

The Enterprise Library Logging Application Block simplifies the implementation of common logging functions. Developers can use the Logging Block to write information to a variety of locations:
  • The event log
  • An e-mail message
  • A database
  • A message queue
  • A text file
  • A WMI event
  • Custom locations using application block extension points

How to Logging Application Block
To add Logging Application Block, we have two phases of steps like

  1. Configuring the Application Block
  2. Writing the Code


Configuring the Application Block

Step 1: Start the Enterprise Library Configuration Console. To use the configuration console, click Start, point to All Programs, point to Microsoft patterns & practices, point to Enterprise Library , and then click Enterprise Library Configuration.
Step 2: Click the New Application icon or the Open Existing Application icon.
Step 3: Right-click Application Configuration, point to New, and then click Logging Application Block.

Selecting the Logging Application Block
After Selecting the Logging Application Block

After you add the Logging Application Block to the application configuration, you need to configure some or all of the following elements:
  • Formatters
  • Trace listeners
  • Special trace sources
  • Category sources
  • Filters
  • The application block

To Configure the Formatters

Formatters have the three options like Binary Formatter, Custom Formatter, and Text Formatter

Binary Formatter

Binary Formatter

Custom Formatter
Custom Formatter

Here there are 3 options like Attributes, Name, Type
In the Attributes we can give the Keys and values.
In the Name Option we can give the Name of the Formatter
In the Type Option We can select the Type (Classes or Namespaces)

Text Formatter

Text Formatter
Template Editor

To configure the trace listeners
Trace Listeners

The Trace Listener having the options like

  • Custom Trace Listener
  • Database Trace Listener
  • Email Trace Listener
  • Flat File Trace Listener
  • Formatted Event Log Trace Listener
  • Msmq Trace Listener
  • System Diagnostics Trace Listener
  • WMI Trace Listener
Each one haves the individual specialization. But now we are discussing about only the Database Trace Listener, Email Trace Listener, And Flat File Trace Listener
Database Trace Listener. This is a trace listener that writes formatted log entries to a database

Database Trace Listener

After click the Database Trace Listener, automatically Data Access Block will be added the screen looks like
Data Access Block configuration Settings

Configure the Connection string in the Data Access Block, and Click on the Database Trace Listener the screen will shows like this
Database Trace Listener Options

Before doing the Database Trace Listener we must have the table to store the log entries.

Email Trace Listener. This is a trace listener that sends out log entries as e-mail.
Selecting the Email Trace Listener

After clicking the Email Trace Listener the Screen looks like
Email Trace Listener Options

Configure the above fields
In the Formatter Choose the Text Formatter
Configure the Fields like FromAddress, Name, SmtpPort, SmtpServer, SubjectLineEnder, SubjectLineStarter, ToAddress, TraceOutputOptions.
Flat File Trace Listener. This is a trace listener that writes log entries to a text file.

Selecting the Flat File Trace Listener

After clicking the Flat File Trace Listener we have to configure the Fields like
Flat File Trace Listener Options

In the FileName we can give the Filename of the Flat File.

To configure the filters

You can filter log entries based on their categories and their priorities. You can also entirely enable or disable logging. The following procedure shows how to configure the filters.

Types of Filters

Category Filter
a. The Category Filter allows or denies log entries based on their categories:
b. Category Filter Options
c. Filter Editor

Custom Filter
a. A Custom Filter is a class that you have created that derives from the LogFilter class. Its configuration information consists of a collection of name/value string pairs. For information about the Custom Filter properties
b. Custom Filter
c. Attributes The key/value pairs for this filter. Click the ellipsis button (…) and use the EditableKeyValue Collection Editor.
d. Name The name of the filter. The default is Custom Filter.
e. Type The type of the trace listener. Select from the drop-down list. This is required.

Log Enabled Filter
a. The LogEnabled Filter must be set to True to permit logging. The following procedure describes how to enable logging and how to change the name of the filter.
b. Log Enabled Filter Options

c. If you want to log events, set the Enabled property to True.
d. (Optional) Change the name of the LogEnabled Filter. For information about the LogEnabled Filter properties

Priority Filter
a. The Priority Filter allows or denies log entries based on their priorities, which are properties. The following procedure describes how to set the Priority Filter properties.
b. Priority Filter Options

c. (Optional) If you want to use a Priority Filter, you can set the MaximumPriority property. This is the maximum priority value a log entry can have to be logged. If you do not set this property, the value is 2147483647 (this is the largest possible value of a 32-bit signed integer). For information about the Priority Filter properties (Optional) You can set the MimimumPriority property. This is the minimum value a log entry must have to be logged. If you do not set this property, the value is -1.
d. Type a new name for the name of the Priority Filter.

To configure the category sources

In the Category Sources node, right-click Category Sources, point to New, and then click Category Source.

a. Category Source Options

b. (Optional) In the right pane, type the name of the category.
c. Click the SwitchValue property and select the severity level of the events that will be assigned to this category. For information about the category source properties
  • Activity Tracing Allows the Stop, Start, Suspend, Transfer, and Resume events through.
  • All Allows all events through.
  • Critical Allows only Critical events through. A critical event is a fatal error or application crash.
  • Error Allows Critical and Error events through. An Error event is a recoverable error.
  • Information Allows Critical, Error, Warning, and Information events through. An information event is an informational message.
  • Off Does not allow any events through.
  • Verbose Allows Critical, Error, Warning, Information, and Verbose events through. A Verbose event is a debugging trace.
  • Warning Allows Critical, Error, and Warning events through. A Warning event is a non-critical problem.

To configure the special trace sources

The Logging Errors & Warnings special source receives log entries for errors and warnings that occur during the logging process. The Unprocessed Category special source receives log entries for that are assigned to categories that are not processed by a category source. The All Events special source receives all log entries.
  1. In the Special Sources node, click the special source you want to use
  2. (Optional) Set the SourceLevel property. For information about the SwitchLevel property values

Writing the Code
1. Set a reference to the Logging Application Block assembly:

  • If you are using C#, in Visual Studio, right-click References in Solution Explorer, and then click Add References to add references to the

    Microsoft.Practices.EnterpriseLibrary.Logging.dll assembly, Microsoft.Practices.EnterpriseLibrary.Common.dll assembly, and Microsoft.Practices.EnterpriseLibrary.ObjectBuilder.dll assembly.

  • If you are using Visual Basic, in Visual Studio, double-click My Project, click the References tab, and then click Add Reference to select the assembly. In the list of imported namespaces at the bottom of the tab, select the following check boxes:

    Microsoft.Practices.EnterpriseLibrary.Logging, Microsoft.Practices.EnterpriseLibrary.Common.dll, and Microsoft.Practices.EnterpriseLibrary.ObjectBuilder.dll.

2. (Optional) To use elements from the Logging Application Block without fully qualifying the element reference, you can add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file.

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;


Example code

The application block supports a small number of classes and methods that simplify the most common logging tasks. Developers can use the following classes to implement common logging scenarios:
  • Logger. This static class writes log information to configurable destinations.
  • LogEntry. This class encapsulates log information.
  • Tracer. This class writes tracing information, such as the start and stop times of activities, to configurable destinations.

[C#]
LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = “Informational message”;
logEntry.Categories.Add(”Trace”);
logEntry.Categories.Add(”UI Events”);
Logger.Write(logEntry);

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • PDF
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Buzz

Tags: ,

4 Responses


  1. Bhaskar on 21 Apr 2009

    Can we use this in a VC++ CLR application. If so how? Can you paste a sample code llike the one you pasted for C# above? Any help regarding this will be appreciated. Please mail me at bhaskar.raghavendran@gmail.com.

  2. Eremeeff on 15 Aug 2009

    Ugh, I liked! So clear and positively.

  3. [...] How to Logging Application Block in Enterprise Library Tagged as: Email Addresses, Search Bar, Tracing Email No Comments The Stages in Filing an Auto Accident Insurance Claim » Comments (0) Trackbacks (0) ( subscribe to comments on this post ) [...]

  4. nursing schools on 31 May 2010

    Keep posting stuff like this i really like it


Leave your comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes