简体   繁体   English

在WCF中记录请求和响应

[英]Logging request and response in WCF

I wanted to understand whether there are any better way to do logging or error handling in customized way with WCF 我想了解是否有更好的方法可以使用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 这是由下面的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. 现在我的问题是,我必须编写30多个函数才能记录上述请求和响应。 Can anybody help me to how I can improve above or need to redesign whole approach. 任何人都可以帮助我提高自身水平或重新设计整个方法。

I've had great success with using PostSharp for logging in my code bases. 使用PostSharp登录我的代码库取得了很大的成功。 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. 在WCF的上下文中,它与Aleksey L建议的IServiceBehavior方法类似,因为它为您提供了在执行该方法之前和之后执行的“钩子”,您可以在其中添加日志记录。 The benefit comes in that you can also use the PostSharp logging attribute outside the context of WCF call. 好处在于,您还可以在WCF调用的上下文之外使用PostSharp日志记录属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM