McG*_*gle 14 c# wcf logging aop custom-attributes
我需要在WCF服务中记录每个方法调用,并抛出任何异常.这导致了许多冗余代码,因为每个方法都需要包含类似于此的样板:
[OperationContract]
public ResultBase<int> Add(int x, int y)
{
var parameters = new object[] { x, y }
MyInfrastructure.LogStart("Add", parameters);
try
{
// actual method body goes here
}
catch (Exception ex)
{
MyInfrastructure.LogError("Add", parameters, ex);
return new ResultBase<int>("Oops, the request failed", ex);
}
MyInfrastructure.LogEnd("Add", parameters);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将所有这些逻辑封装到一个属性中MyServiceLoggingBehaviorAttribute,我可以将其应用于服务类(或方法),如下所示:
[ServiceContract]
[MyServiceLoggingBehavior]
public class MyService
{
}
Run Code Online (Sandbox Code Playgroud)
注意#1
我意识到这可以使用面向方面的编程来完成,但在C#中,唯一的方法是修改字节码,这需要使用像PostSharp这样的第三方产品.我想避免使用商业图书馆.
笔记2
请注意,Silverlight应用程序是该服务的主要使用者.
注意#3
在某些情况下,WCF跟踪日志记录是一个不错的选择,但它在这里不起作用,因为如上所述,我需要检查,并且在异常更改的情况下,返回值.
McG*_*gle 27
是的,可以使用WCF内置的扩展点来封装这种日志记录.实际上有多种可能的方法.我在这里描述的IServiceBehavior那个添加了一个,它使用自定义IOperationInvoker,并且不需要任何web.config修改.
这有三个部分.
IOperationInvoker,它将方法调用包装在所需的日志记录和错误处理中.IOperationBehavior适用于步骤1中的调用者的实现.IServiceBehavior继承Attribute自第2步的行为,并应用该行为.IOperationInvoker的关键是Invoke方法.我的类将基本调用程序包装在try-catch块中:
public class LoggingOperationInvoker : IOperationInvoker
{
IOperationInvoker _baseInvoker;
string _operationName;
public LoggingOperationInvoker(IOperationInvoker baseInvoker, DispatchOperation operation)
{
_baseInvoker = baseInvoker;
_operationName = operation.Name;
}
// (TODO stub implementations)
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
MyInfrastructure.LogStart(_operationName, inputs);
try
{
return _baseInvoker.Invoke(instance, inputs, out outputs);
}
catch (Exception ex)
{
MyInfrastructure.LogError(_operationName, inputs, ex);
return null;
}
MyInfrastructure.LogEnd("Add", parameters);
}
}
Run Code Online (Sandbox Code Playgroud)
IOperationBehavior的实现只是将自定义调度程序应用于操作.
public class LoggingOperationBehavior : IOperationBehavior
{
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Invoker = new LoggingOperationInvoker(dispatchOperation.Invoker, dispatchOperation);
}
// (TODO stub implementations)
}
Run Code Online (Sandbox Code Playgroud)
此实现IServiceBehavior将操作行为应用于服务; 它应该继承,Attribute以便它可以作为属性应用于WCF服务类.这方面的实施是标准的.
public class ServiceLoggingBehavior : Attribute, IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
IOperationBehavior behavior = new LoggingOperationBehavior();
operation.Behaviors.Add(behavior);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用Audit.NET库及其Audit.WCF扩展。它可以记录 WCF 服务交互并兼容异步调用。
您需要做的就是使用以下AuditBehavior属性装饰您的 WCF 服务类或方法:
[AuditBehavior()]
public class OrderService : IOrderService
{ ... }
Run Code Online (Sandbox Code Playgroud)
WCF 扩展使用IOperationInvoker实现Invoke和InvokeBegin/ InvokeEnd。您可以在此处查看代码。
| 归档时间: |
|
| 查看次数: |
13401 次 |
| 最近记录: |