簡體   English   中英

在WCF中記錄請求和響應

[英]Logging request and response in WCF

我想了解是否有更好的方法可以使用WCF以自定義方式進行日志記錄或錯誤處理

這是場景。 我有以下服務

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);
    }
}

這是由下面的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;
        }
    }
}

記錄功能如下

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

現在我的問題是,我必須編寫30多個函數才能記錄上述請求和響應。 任何人都可以幫助我提高自身水平或重新設計整個方法。

使用PostSharp登錄我的代碼庫取得了很大的成功。 在WCF的上下文中,它與Aleksey L建議的IServiceBehavior方法類似,因為它為您提供了在執行該方法之前和之后執行的“鈎子”,您可以在其中添加日志記錄。 好處在於,您還可以在WCF調用的上下文之外使用PostSharp日志記錄屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM