What is Logging?
When to Use the Logging Application Block
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 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
- Configuring the Application Block
- Writing the Code
Configuring the Application Block
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.
- Formatters
- Trace listeners
- Special trace sources
- Category sources
- Filters
- The application block
To Configure the Formatters
Binary Formatter
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
To configure the 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
Database Trace Listener. This is a trace listener that writes formatted log entries to a database
After click the Database Trace Listener, automatically Data Access Block will be added the screen looks like
Configure the Connection string in the Data Access Block, and Click on the Database Trace Listener the screen will shows like this
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.
After clicking the Email Trace Listener the Screen looks like
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.
After clicking the Flat File Trace Listener we have to configure the Fields like
In the FileName we can give the Filename of the Flat File.
To configure the filters
Category Filter
a. The Category Filter allows or denies log entries based on their categories:
b.
c.
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.
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.
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.
To configure the category sources
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
- In the Special Sources node, click the special source you want to use
- (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.
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
Example code
- 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);

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.
Ugh, I liked! So clear and positively.
[...] 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 ) [...]
Keep posting stuff like this i really like it