2

我有产品服务。在每次调用服务时,我都想调用一个方法。在这种情况下,我正在记录。我正在寻找一种方法,而不是在每个方法中都写 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 回答