Saturday, February 19, 2011

enterprise library 5.0 Logging With WCF

Few days back I was asked to Integrate The Application Block Logging with WCF TraceListener.

I searched The Net No Usefull Tutorial was Found So good Leads but They where not Integrating Application Block with So I decided to Write My Own.
The code is very simple

So let Start,

1 } I Using Vs2010 & Enterprise Libary
I Have used

Downloaded Libary from above link.

2} Create WCF Application

3} Know Edit The Web.config File

4 } You Will See The Window For Editing

5} Selecting Rolling Filat File Logging

6} Remove The Default Listener

7} Select Rolling Flat File

8} Do the Setting For Rolling Flat File

9} Save and Move to web.config file add Digonistic Tag

10} Adding Diagontsic to to ServiceModel Tag

11} Know Create cs File Which would work as Trace Listener and Refrence file for Microsoft Enterprise Libary

12 } Assemblies Selected from bin

13} Code Used For Credating Listener

14} Know Created The Website and Add service Refernce for Testing

15} Code Used for Calling Service Refernce

16} When We Click Simple Button

17} Know Checking The data Which is Requested from Flat file

18 } Checking Response in Rolling Flat file

Know Code of web.config, TraceListener, & WCf service

19} Web.config code

<?xml version="1.0"?>

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<add name="xml" />
<source name="System.ServiceModel.MessageLogging">
<add name="xml" />
<add name="xml" type="WCFApplicationLogging.WebTraceListener,WCFApplicationLogging" />

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="E:\EnterPrise_Libary\Sample\rolling.log" rollFileExistsBehavior="Increment"
rollSizeKB="124" maxArchivedFiles="1200" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
<add switchValue="All" name="General">
<add name="Rolling Flat File Trace Listener" />
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<add name="Rolling Flat File Trace Listener" />
<compilation debug="true" targetFramework="4.0" />

<bindings />
<client />
<messageLogging logEntireMessage="true"

<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<modules runAllManagedModulesForAllRequests="true"/>


20} WebTraceListener.cs

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using System.Diagnostics;
using System.Collections.Generic;

namespace WCFApplicationLogging
public class WebTraceListener : TraceListener
private LogWriter logWriter = null;
private TraceManager traceManager = null;
IDictionary categories = null;

public WebTraceListener()
this.logWriter = EnterpriseLibraryContainer.Current.GetInstance();
this.traceManager = EnterpriseLibraryContainer.Current.GetInstance();
this.categories = this.logWriter.TraceSources;

public override void Write(string message)
this.logWriter.Write(message, "Avinash test");

public override void WriteLine(string message)
this.logWriter.Write(message, "Avinash test");

20 } WCF service

I have used Bolier plate code Given by Microsoft so it is default code.


  1. This comment has been removed by the author.

    1. at step 8 i am getting the error'the configuration has validation errors....'

    2. Seems u have to reinstall the Enterprise Libary

  2. Bonjour,
    how to change the settings of a configuration?
    for example, change the file path in run time