Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

LoggingBehavior - How to Connect Log-prints with the WCF Operation's Details, using a Simple Behavior

, 27 Sep 2011 CPOL
In this article, I explain, step by step, how we can use a WCF behavior for logging operation's invoke and result, errors, warnings and information logs that include the operation's details.
WcfLogPrints_Demo.zip
WcfLogPrints_Demo
Client
Example.Client.exe
Example.Client.pdb
Example.Contracts.dll
Example.Contracts.pdb
Server
Example.Contracts.dll
Example.Contracts.pdb
Example.Server.exe
Example.Server.pdb
Example.Services.dll
Example.Services.pdb
WcfLogPrints.dll
WcfLogPrints.pdb
WcfLogPrints_src.zip
WcfLogPrints
Example.Client
bin
Debug
Properties
Example.Contracts
bin
Release
Properties
Example.Server
bin
Debug
Properties
Example.Services
bin
Release
Properties
WcfLogPrints
bin
Release
Properties
WcfLogPrints.suo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace WcfLogPrints
{
    public class LoggingBehaviorAttribute : Attribute, IServiceBehavior, IOperationBehavior
    {
        public LoggingBehaviorAttribute()
        {
            LogBeforeCall = true;
            LogAfterCall = true;
            LogErrors = true;
            LogWarnings = true;
            LogInformation = true;
        }

        #region IServiceBehavior Members

        public void AddBindingParameters(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase,
            Collection<ServiceEndpoint> endpoints,
            BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
        {
            LoggingParameterInspector paramInspector = new LoggingParameterInspector
            {
                ServiceType = serviceDescription.ServiceType,
                LoggingStrategy = GetLoggingStrategy(),
                LogAfterCall = LogAfterCall,
                LogBeforeCall = LogBeforeCall,
                LogErrors = LogErrors,
                LogWarnings = LogWarnings,
                LogInformation = LogInformation
            };

            foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
                {
                    foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
                    {
                        op.ParameterInspectors.Add(paramInspector);
                    }
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
        {
        }

        #endregion

        #region IOperationBehavior Members

        public void AddBindingParameters(OperationDescription operationDescription,
            BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription,
            ClientOperation clientOperation)
        {
            LoggingParameterInspector paramInspector = new LoggingParameterInspector
            {
                ServiceType = operationDescription.DeclaringContract.ContractType,
                LoggingStrategy = GetLoggingStrategy(),
                LogAfterCall = LogAfterCall,
                LogBeforeCall = LogBeforeCall,
                LogErrors = LogErrors,
                LogWarnings = LogWarnings,
                LogInformation = LogInformation
            };

            clientOperation.ParameterInspectors.Add(paramInspector);
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription,
            DispatchOperation dispatchOperation)
        {
            LoggingParameterInspector paramInspector =
                dispatchOperation.ParameterInspectors.FirstOrDefault(
                    pi => pi.GetType() == typeof(LoggingParameterInspector)) as LoggingParameterInspector;

            if (paramInspector != null)
            {
                // The logging inspector already exist...

                dispatchOperation.ParameterInspectors.Remove(paramInspector);
            }

            paramInspector = new LoggingParameterInspector
            {
                ServiceType = operationDescription.DeclaringContract.ContractType,
                LoggingStrategy = GetLoggingStrategy(),
                LogAfterCall = LogAfterCall,
                LogBeforeCall = LogBeforeCall,
                LogErrors = LogErrors,
                LogWarnings = LogWarnings,
                LogInformation = LogInformation
            };

            dispatchOperation.ParameterInspectors.Add(paramInspector);
        }

        public void Validate(OperationDescription operationDescription)
        {
        }

        #endregion

        #region Properties

        public bool LogBeforeCall { get; set; }
        public bool LogAfterCall { get; set; }
        public bool LogErrors { get; set; }
        public bool LogWarnings { get; set; }
        public bool LogInformation { get; set; }

        #region LoggingStrategyType
        private Type _loggingStrategyType;
        public Type LoggingStrategyType
        {
            get { return _loggingStrategyType; }
            set
            {
                if (value != null &&
                    !value.GetInterfaces().Contains(typeof(ILoggingStrategy)))
                {
                    throw new ArgumentException("The specified type is not instance of ILoggingStrategy.");
                }

                _loggingStrategyType = value;
            }
        }
        #endregion

        #endregion

        private ILoggingStrategy GetLoggingStrategy()
        {
            if (LoggingStrategyType != null)
            {
                return Activator.CreateInstance(LoggingStrategyType) as ILoggingStrategy;
            }

            return new ConsoleLoggingStrategy();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shmuel Zang
Software Developer
Israel Israel
No Biography provided
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 27 Sep 2011
Article Copyright 2011 by Shmuel Zang
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid