Tagged: profile
PostSharp – Simple profiling aspect
I recently came across an example of how to use PostSharp to create a profiling aspect (source).
The following example takes advantage of the OnMethodBoundaryAspect class.
[Serializable] [ProfilerAspect(AttributeExclude = true)] public class ProfilerAspect : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { args.MethodExecutionTag = Stopwatch.StartNew(); } public override void OnExit(MethodExecutionArgs args) { Stopwatch sw = (Stopwatch)args.MethodExecutionTag; sw.Stop(); string output = string.Format("{0} Executed in {1} milliseconds", args.Method.Name, sw.ElapsedMilliseconds); System.Diagnostics.Debug.WriteLine(output); } }
Apply the aspect on the assembly:
[assembly: ProfilerAspect()]
If you choose to apply the aspect on the entire assembly, set the AttributeExclude to true on the aspect itself.
Don’t forget to reference the PostSharp assembly…and it’s done.