Introduction
I have been using various Microsoft Enterprise Library application blocks in my application development. I found the Logging Application Block very easy to integrate and utilize specially to log in the Application Event Log of the system. The Enterprise Library Logging Application Block simplifies the implementation of common logging functions.
We log application exceptions in some or other way. Many of us use some standard logging applications to do so. Let me share how to integrate and use the Logging Application Block using a simple ASP.Net web application on dotnet framework 3.5.
Please note that I have used the Microsoft Enterprise Library 4.1 – October, 2008 release. It works on .net framework 3.5 and Visual Studio 2008.
You can download Microsoft Enterprise Library 4.1 from the URL:
http://www.microsoft.com/downloads/details.aspx?FamilyId=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en
Integration and Usage:
My Application: A simple ASP.Net application using dotnet framework 3.5. The application has a web page that takes in user input for Dividend, Divisor and with Divide button click divides the values and shows the Quotient in the Quotient textbox and a class to log the events. Refer the attached application.
I have used this example to trap error for the condition dividebyzero. If the user inputs 0 in Divisor field, it would throw an exception. I have logged the exceptions and standard information in the Application Event Log of the system.
Steps:
First, we need to install the Microsoft Enterprise Library 4.1 and get hold of the assembly “Microsoft.Practices.EnterpriseLibrary.Logging.dll” and add the reference of the same in our project. In the attached project, you will find the dll inside the folder “Enterprise Logging Library”. So in such a case you can skip the download to get the assembly if you don’t want to download.
Next you need to add few configuration in the web.config file. Refer loggingConfiguration section under the “configSections” and loggingConfiguration node in the web.config file of the attached project.
Then add the class to actually log information in Application Event Log. Refer the “EventLog.cs” class in the attached project.
public void LogData(string vstrMessage, TraceEventType enmEventType)
{
LogEntry objLog = new LogEntry();
objLog.Message = vstrMessage;
objLog.Categories.Add("General");
objLog.Severity = enmEventType;
Logger.Write(objLog);
}
The above method uses the LogEntry and Logger class provided by the application block to log into the Application Event Log.
Then in my web page’s divide button click event I used the EventLog to log data into the Application Event Log. Refer the “Default.aspx” and “Default.aspx.cs” in the attached project.
Then execute the application and run through positive (e.g. Dividend = 10 and Divisor = 2) and negative (e.g. Dividend = 10 and Divisor = 0) scenarios and check the Application Event Log in the Event Viewer of the system, you will get to see something like the following screenshots.
The above screenshot is for positive scenario.
The above screenshot is for negative scenario.
Please note that in the positive scenario, I have logged Information and with negative scenario, I have logged an Error in the Application Event Log of the system.
Conclusion:
By now, you can understand how simple it is to use the Enterprise Library 4.1 application blocks for Logging.
To study more on Microsoft Enterprise Library 4.1 application blocks, refer the URL http://msdn.microsoft.com/en-us/library/dd203099.aspx