-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformanceAspect.cs
38 lines (33 loc) · 1.05 KB
/
PerformanceAspect.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
using Castle.DynamicProxy;
using Core.Utilities.Interceptors;
using Core.Utilities.IoC;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
namespace Core.Aspects.Autofac.Performance
{
public class PerformanceAspect : MethodInterception
{
private int _interval;
private Stopwatch _stopwatch;
public PerformanceAspect(int interval)
{
_interval = interval;
_stopwatch = ServiceTool.ServiceProvider.GetService<Stopwatch>();
}
protected override void OnBefore(IInvocation invocation)
{
_stopwatch.Start();
}
protected override void OnAfter(IInvocation invocation)
{
if (_stopwatch.Elapsed.TotalSeconds > _interval)
{
Debug.WriteLine($"Performance : {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}-->{_stopwatch.Elapsed.TotalSeconds}");
}
_stopwatch.Reset();
}
}
}