2

I am using the mvc mini profiler to profile a NUnit Test suite. I am just curious whether it would be possible to use the mvc mini profiler's profiling mechanism as an aspect, i.e., rather than having those using statements could I not somehow just provide some attribute above the method I want to profile? I know that this would kill the kind of granularity we get with the mini profiler but in some cases, it's just more appropriate to use the AOP approach.

Ideas? Suggestions?

Thanks a bunch.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Kumar Vaibhav
  • 2,632
  • 8
  • 32
  • 54

2 Answers2

3

You would need to weave in code, so you would have to look at either PostSharp, Roslyn or some other IL weaving mechanism.

Community
  • 1
  • 1
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • Taking a look at Roslyn for the first time. PostSharp I don't want to use even though their starter edition is free. I wanted to use something that's open source, free and uses IL code weaving rather than use proxies. Seems dot net doesn't offer much in this area. – Kumar Vaibhav Jan 01 '13 at 05:55
3

Yes, it is totally possible. In my case I was using Autofac, which implements interception using Castle's DynamicProxy.

But a very basic interceptor for profiling would look something like this (in C#):

public class ProfilerInterceptor : IInterceptor
{
    #region Implementation of IInterceptor

    public void Intercept(IInvocation invocation)
    {
        using (MiniProfiler.Current.Step(invocation.TargetType.Name + "." + invocation.Method.Name))
        {
            invocation.Proceed();
        }
    }

    #endregion
}

NOTE: I know your preference was weaving rather than interception through proxies, but I'm posting it in case anybody else finds it useful.

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58