A day with .Net

My day to day experince in .net

WCF Logging with ELMAH

Posted by vivekcek on June 24, 2014

In this post, i will show you how to use ELMAH for WCF logging.

1. Create a WCF application.

2. Through Nuget add ELMAH EveryWhere. (Link)

3. Download ELMAH’s SQL Script and create a database. (Link)

4. Create a class ElmahErrorHandler which implementIErrorHandler.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Dispatcher;
using System.Web;

namespace DVDService
{
    public class ElmahErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            return false;
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            if (error == null)
            {
                return;
            }
            if (HttpContext.Current == null)
            {
                return;
            }
            Elmah.ErrorSignal.FromCurrentContext().Raise(error);
        }
    }
}

5. Create a Service behaviour attribute ServiceErrorBehaviorAttribute.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Web;

namespace DVDService
{
    public class ServiceErrorBehaviorAttribute : Attribute, IServiceBehavior
    {
        private readonly Type errorHandlerType;

        public ServiceErrorBehaviorAttribute(Type errorHandlerType)
        {
            this.errorHandlerType = errorHandlerType;
        }
        
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            IErrorHandler errorHandler;
            errorHandler = Activator.CreateInstance(errorHandlerType) as IErrorHandler;
            if (errorHandler != null)
            {
                foreach (ChannelDispatcherBase dispatcher in serviceHostBase.ChannelDispatchers)
                {
                    ChannelDispatcher cd = dispatcher as ChannelDispatcher;
                    cd.ErrorHandlers.Add(errorHandler);
                }
            }
        }

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

6. In Web.config just after add the below tags. Update your connection string.

<configSections>
    <sectionGroup name="elmah">
      <section name="errorLog" requirePermission="false"
                                           type="Elmah.ErrorLogSectionHandler, Elmah"/>
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="elmah" connectionString="Data Source=.\sqlexpress;Initial Catalog=ELMAH;Integrated Security=True"/>
  </connectionStrings>
  <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah"/>
  </elmah>

7. In Web.config under add the tags below.

<handlers>
      <add name="ELMAH" verb="POST,GET,HEAD" path="elmah/default.aspx" type="Elmah.ErrorLogPageFactory, Elmah"/>
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </modules>

8. Under make sure this is correct.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

9. Now implement your service

 [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DVDService : IDVDService
    {
      public List<Movie> GetAllData()
        {
            throw new ApplicationException("TEST");
        }
    }

10. To view your exceptions http://localhost/YourService.svc/elmah/default.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s