Created
February 8, 2015 10:10
-
-
Save jimmason/23f993d5b8bd27d3c216 to your computer and use it in GitHub Desktop.
PostSharp Timing Aspect Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using PostSharp.Aspects; | |
using PostSharp.Extensibility; | |
namespace PostSharpAspectExample | |
{ | |
[Serializable] | |
[MulticastAttributeUsage(MulticastTargets.Method)] | |
public class TimingAspect : OnMethodBoundaryAspect | |
{ | |
[NonSerialized] private Stopwatch stopWatch; | |
public override void OnEntry(MethodExecutionArgs methodExecutionArgs) | |
{ | |
stopWatch = Stopwatch.StartNew(); | |
base.OnEntry(methodExecutionArgs); | |
} | |
public override void OnExit(MethodExecutionArgs methodExecutionArgs) | |
{ | |
Console.WriteLine("{0} took {1}ms" | |
, new StackTrace().GetFrame(1).GetMethod().Name | |
, stopWatch.ElapsedMilliseconds); | |
base.OnExit(methodExecutionArgs); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment