-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathMiniProfilerStepAttribute.cs
47 lines (39 loc) · 1.15 KB
/
MiniProfilerStepAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using PostSharp.Aspects;
using PostSharp.Serialization;
using StackExchange.Profiling;
using System;
using System.Reflection;
namespace PostSharp.Samples.MiniProfiler
{
[PSerializable]
[LinesOfCodeAvoided(2)]
public sealed class MiniProfilerStepAttribute : OnMethodBoundaryAspect
{
private string methodName;
public MiniProfilerStepAttribute()
{
SemanticallyAdvisedMethodKinds = SemanticallyAdvisedMethodKinds.Async;
}
public override bool CompileTimeValidate(MethodBase method)
{
// Don't apply the aspect to constructors, property getters, and so on.
if (method.IsSpecialName)
{
return false;
}
return true;
}
public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
{
methodName = method.DeclaringType.Name + "." + method.Name;
}
public override void OnEntry(MethodExecutionArgs args)
{
args.MethodExecutionTag = StackExchange.Profiling.MiniProfiler.Current?.Step(methodName);
}
public override void OnExit(MethodExecutionArgs args)
{
((IDisposable) args.MethodExecutionTag)?.Dispose();
}
}
}