private void InterceptTaskWithResult <TResult>(IInvocation invocation) { var ctx = new ProfilingContext(); var invocationClone = invocation.Clone(); invocation.ReturnValue = Task.Run(() => BeforeInvoke(ctx, invocation)) .ContinueWith(t => { if (t.Exception != null) { throw t.Exception; } invocationClone.Proceed(); return(invocationClone.ReturnValue as Task <TResult>); }) .Unwrap() .ContinueWith(t => { if (t.Exception != null) { throw t.Exception; } invocationClone.ReturnValue = t.Result; AfterInvoke(ctx, invocationClone); return((TResult)invocationClone.ReturnValue); }); }
void IInvocationProfiler.BeginMeasure(ProfilingContext ctx, IInvocation invocation) { string typeName = invocation.Request.Method.DeclaringType.FullName; string methodName = invocation.Request.Method.Name; ctx.Timer = Stopwatch.StartNew(); ctx.Data = new List <object>(invocation.Request.Arguments); ctx.Data.Add($"{typeName}.{methodName}"); }
void IInvocationProfiler.Measure(IInvocation invocation) { var ctx = new ProfilingContext(); var profiler = this as IInvocationProfiler; profiler.BeginMeasure(ctx, invocation); invocation.Proceed(); profiler.EndMeasure(ctx, invocation); }
void IInvocationProfiler.EndMeasure(ProfilingContext ctx, IInvocation invocation) { ctx.Timer.Stop(); ctx.Data.Add(invocation.ReturnValue); var snapshot = new Snapshot(ctx.Timer.Elapsed, ctx.Data.ToArray()); _snapshots.Add(snapshot); }
/// <summary> /// Takes profile action after the invocation proceeds /// </summary> /// <param name="ctx">The context of current profiling</param> /// <param name="invocation">The invocation that is being intercepted</param> private void AfterInvoke(ProfilingContext ctx, IInvocation invocation) { Profiler.EndMeasure(ctx, invocation); }
/// <summary> /// Takes profile action before the invocation proceeds /// </summary> /// <param name="ctx">The context of current profiling</param> /// <param name="invocation">The invocation that is being intercepted</param> private void BeforeInvoke(ProfilingContext ctx, IInvocation invocation) { Profiler.BeginMeasure(ctx, invocation); }