0

I wanted to understand whether there are any better way to do logging or error handling in customized way with WCF

Here is the scenario. I have a service as below

namespace IntegrationServices.Contract.SomeServices
{
    [ServiceContract(Name = "SomeServices")]
    public interface ISomeService
    {
        //Having 30+ contracts below is one of them

        [OperationContract]
        [WebInvoke(UriTemplate = "/GetOnlineSomething")]
        SomeTransactionResponse GetOnlineSomething(string someNumber);
    }
}

Which is implemented by below calss

namespace IntegrationServices.Service.PaymentServices
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [GlobalErrorBehaviorAttribute(typeof(GlobalErrorHandler), Project.Name)]
    public class PaymentService : ISomeService
    {
    public OnlinePaymentTransactionResponse GetOnlinePaymentTransaction(string someNumber)
        {
            //we have authentication code here which is OK
            //Logging the request
            _transactionKey = Guid.NewGuid();
            TransactionRequest(/*some message and some parameter*/);            

            try
            {
                //do something
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLogAsync(/*logging some more information*/);
                response.ErrorMessage = Project.PHAPICommonErrorMessage;
            }

            //Logging the response
            TransactionResponse(/*some parameter and error message from catch block*/);

            return response;
        }
    }
}

Logging Function is as below

private void TransactionRequest(string xmlObject, Guid? groupKey, string name)
        {
            //writing to DB 
        }
private void TransactionResponse(string xmlObject, Guid? groupKey, string name)
        {
            //writing to DB 
        }

Now my question here is, I have to write in all 30+ function to log request and response like above. Can anybody help me to how I can improve above or need to redesign whole approach.

LifeOfPi
  • 625
  • 5
  • 19
  • 4
    Use wcf extensibility points http://stackoverflow.com/questions/13655541/wcf-service-attribute-to-log-method-calls-and-exceptions – Aleksey L. Jun 19 '16 at 06:16

1 Answers1

0

I've had great success with using PostSharp for logging in my code bases. In the context of WCF its similar to the IServiceBehavior approach suggested by Aleksey L in that it gives you "hooks" that execute before and after the method's execution in which you can add your logging. The benefit comes in that you can also use the PostSharp logging attribute outside the context of WCF call.

Stephen McDowell
  • 839
  • 9
  • 21