2

製品サービスがあります。Service を呼び出すたびに、メソッドを呼び出したいと思います。この場合、ログを記録しています。各メソッドに using ステートメントを記述しない方法を探しています。しかし、私はまだ各呼び出しでロギングを行いたいと思っています。どうすればいいですか?

    public class ProductService : IProductService
{
    public IList<Product> GetProductsByBrand(int BrandID)
    {
        using (new Logging())
        {
            // Get a list of products By Brand
        }
        return new List<Product>();
    }

    public IList<Product> Search(string ProductName)
    {
        using (new Logging())
        {
            // Search
        }
        return new List<Product>();
    }

    public static string OrderProducts(IList<Order> Orders, Payment paymentDetials)
    {
        string AuthCode;
        using (new Logging())
        {
            // Order and get the AuthCode
        }
        AuthCode = "";
        return AuthCode;
    }
}
4

3 に答える 3

3

AOP (アスペクト指向プログラミング) について聞いたことがありますか? これは、対象の型をラップし、ラップするメソッドの前後に追加の処理を実行する再利用可能なアスペクトとしてクロス カットの懸念を実装する方法です。

http://en.wikipedia.org/wiki/Decorator_pattern

WCF 環境内では、これは通常、サービス クラスに「ビヘイビア」を適用することによって行われます。この場合、IParameterInspector を実装する属性を使用して IOperationBehavior インターフェイスを提案し、サービス インスタンスが作成されて呼び出される前にパラメーターを確認します。wcf メッセージ パイプラインを拡張するためのオプションについて詳しく説明している便利な記事へのリンクを次に示します。

http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

//Attribute class
public class LogOperationBehavior : Attribute, IOperationBehavior, IParameterInspector {

public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) {
    return;
}

public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation) {
    //clientOperation.ParameterInspectors.Add(new ClientParameterInspector());            
}

public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) {
    dispatchOperation.ParameterInspectors.Add(this);
}

public void Validate(OperationDescription operationDescription) {
    return;
}



#region IParameterInspector Members

public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) {
   //perform logging after
}

public object BeforeCall(string operationName, object[] inputs) {
    //perform logging before
    return null;
}

#endregion

}

  public class BusinessOperation : IBusinessOperation {

    //Apply to your service via an attribute
    [LogOperationBehavior]
    public DivideResponse DivideTwoNumbers(DivideRequest dr) {            
        return new DivideResponse() {
            Answer = dr.Numerator/ dr.Demoninator2,              
        };
    }
于 2012-06-24T21:19:48.303 に答える
1

ロギング プロキシの作成を検討しましたか? 次のようになります。

public class LoggingProductService : IProductService
{
    private readonly IProductService _core;

    public LoggingProductService(IProductService core)
    {
        _core = core;
    }

    public IList<Product> GetProductsByBrand(int BrandID)
    {
        Log("Getting products for brand " + BrandId);
        return _core.GetProductsByBrand(BrandId);
    }

    //other IProductService methods here, all logging and delegating to _core

    private void Log(string message)
    {
        using (var log = new Logging())
        {
            log.Write(message);
        }
    }
}

もちろん、私はあなたの Logging インターフェースを完全には理解していないので、適切な推測を正しいコードで埋めてください。また、頻繁にロギングを作成および破棄したくない場合もありますが、わかりません。

于 2012-06-23T22:53:02.607 に答える
0

動的プロキシを作成できます。手順については、この記事を参照してください。 http://www.drdobbs.com/windows/184405378

于 2012-06-24T03:29:28.943 に答える