【问题标题】:measure execution time using interceptors使用拦截器测量执行时间
【发布时间】:2015-01-31 05:54:05
【问题描述】:

我正在拦截我在我的应用程序中托管的服务的 wcf 调用,以显示有关服务的数据(每个方法处理了多少调用,记录方法参数等)。除此之外,我还想测量每个方法的执行时间。

我已经用 Castle 拦截器包装了实例,我使用 StopWatch 来测量同步方法。

但是,我不能在异步方法上使用秒表,因为我有 2 种不同的方法(对于 beginInvokeEndInvoke,我拦截了 WCF 调用程序)。

如何测量执行时间,或者至少关联 Begin 和 End 的调用?

谢谢

【问题讨论】:

    标签: c# aop interceptor castle


    【解决方案1】:

    听起来这个问题已经在这里回答了Using the Stopwatch with Async methods

    干杯

    【讨论】:

    • 也许我不清楚,所以我改进了我的问题。谢谢
    【解决方案2】:

    知道了,

    我意识到invocation.ReturnValue 拥有任务,所以我所要做的就是实现TaskInterceptor

    public class TaskInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var task = invocation.ReturnValue as Task;
    
            InterceptBeforeInvoking();
    
            invocation.Proceed();
    
            task.ContinueWith(InterceptAfterInvocation);
    
    
        }
    
        private void InterceptAfterInvocation(Task obj)
        {
    
    
        }
    
        private void InterceptBeforeInvoking()
        {
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多