Configuring WCF service to utilise Enterprise Library: Logging application to log data to database

This article walks through the 'Enterprise Library: Logging application' configuration process to log data to database using WCF service.

Note that regardless of Web service type i.e., SOAP or RESTful the configuration process is same.

Technical environment:-

Visual Studio 2010 with .NET 4.0
Enterprise library 5.0
WCF

ASP.NET Web application on Client end.

Open Service Web.config using Enterprise library configuration tool

Open the Service Web configuration file using Enterprise library configuration tool, which can be found at C:\Program Files\Microsoft Enterprise Library 5.0\Bin [assuming that you chosen C: drive for Enterprise library installation process]

EntLib_Configuration_Tool

[Image: Enterprise library configuration tool location]

Note: Make sure that Service Web.config contains database connection string in connectionStrings section.

Step 1: Open Enterprise library configuration tool

Below screenshot shows the configuration tool after opening the service web.config file.

Service_Web_Configuration_Initail
[Service web.configuration with in Enterprise Library configuration tool]

Step 2: _Choosing the Default database instance with in Database settings section

Choose the Default Database Instance from drop down list, in this example Logging is chosen as default one as shown below.

Default_Database_Instance

[Default Database Instance]

Step 3: Add Logging block block

Choose 'Logging Settings' to add logging application block from Blocks menu as shown below screenshot.

Add_Logging_Block

[Add_Logging_Block]

Step 4: Expand Logging Settings section

In order to configure Logging block, expand logging settings by clicking the icon on the  left hand side as marked in red below image.

Expand_Logging_Settings

[Expand_Logging_Section_Image]

Step 5: Add Database Trace listener

From Logging Target Listeners selecting the + opens 'Add Logging Target  Listeners'.

Choose 'Add Database Trace Listener' from context menu that is opened from
'Add Logging Target Listeners' as shown below screenshot.

Add_Database_Trace_Listener

[Add-database-trace-listener]

Step 6: Choose Database Trace Listener as default listener

Make sure that 'Database Trace listener' is chosen as default listener in Logging settings --> General section as shown in below image.

Choose_Database_Trace_Listener_As_Default

[Choose-db-trace-listener]

Note:- By selecting right mouse button on particular configuration section it is feasible to Validate particular section as shown in below image.

Config_Sections_Validate

[Configuration-Sections-Validate]

Step 7: Enterprise library application block DLL references

Make sure that below mentioned references from Enterprise library API are added to
the service WCF service as shown in below image.

These DLLs are available from Enterprise Library installation at

C:\Program Files\Microsoft Enterprise Library 5.0\Bin

Required-DLL-References

[Required DLLs]

Step 8: Add required 'using' statements

 

   1: using Microsoft.Practices.EnterpriseLibrary.Logging;
   2: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
   3: using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
   4: using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
   5: using Microsoft.Practices.EnterpriseLibrary.Data;
   6: using System.Data;

Step 9: Create a method that utilises Logging application block to log data to database.

Create a private LogWriter variable as below;

   1: private LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); 

Logger method can be similar to the one below or it can have more parameters.

   1: ///Create a logEntry object and write to database
   2: public void  DbLogger(string _Title, string _Message, string _MachineName)
   3: {
   4:     LogEntry logEntry = new LogEntry();
   5:  
   6:     logEntry.Title = _Title;
   7:     logEntry.Message = _Message;
   8:     logEntry.MachineName = _MachineName;
   9:     logEntry.TimeStamp = DateTime.Now;
  10:  
  11:     writer.Write(logEntry);
  12: }

Step 10: Test the functionality with a client instance

   1: protected void Page_Load(object sender, EventArgs e)
   2:         {
   3:             //Create an instance of client proxy 
   4:             Service1Client client = new Service1Client();
   5:             client.Open();
   6:  
   7:             try
   8:             {
   9:                 client.DbLogger("Logger service consumer", "Testing the Logger db functionality", "My machine");
  10:             }
  11:  
  12:             catch (Exception ex)
  13:             {
  14:                 Response.Write(ex.InnerException.Message.ToString());
  15:             }
  16:         }

Running the client app with above should log data to Logging database, which can be installed from scripts provided with Hands-on-Labs referenced below.

image

Creating Logging database

Note that Logging database can be created by running the SQL script provided with Enterprise Library 5.0 Hands on labs, which can be downloaded at
http://www.microsoft.com/download/en/details.aspx?id=6932

Reference

http://msdn.microsoft.com/en-us/library/ff632023.aspx

http://www.microsoft.com/download/en/details.aspx?id=6932

http://msdn.microsoft.com/en-us/library/ff650510.aspx

No Comments