-
Notifications
You must be signed in to change notification settings - Fork 11
/
LogAspect.cs
53 lines (46 loc) · 1.58 KB
/
LogAspect.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
48
49
50
51
52
53
using Castle.DynamicProxy;
using Core.CrossCuttingConcerns.Logging;
using Core.CrossCuttingConcerns.Logging.Log4Net;
using Core.Utilities.Interceptors;
using Core.Utilities.Messages;
using System;
using System.Collections.Generic;
using System.Text;
namespace Core.Aspects.Autofac.Logging
{
public class LogAspect : MethodInterception
{
private LoggerServiceBase _loggerServiceBase;
public LogAspect(Type loggerService)
{
if (loggerService.BaseType != typeof(LoggerServiceBase))
{
throw new System.Exception(AspectMessages.WrongLoggerType);
}
_loggerServiceBase = (LoggerServiceBase)Activator.CreateInstance(loggerService);
}
protected override void OnBefore(IInvocation invocation)
{
_loggerServiceBase.Info(GetLogDetail(invocation));
}
private LogDetail GetLogDetail(IInvocation invocation)
{
var logParameters = new List<LogParameter>();
for (int i = 0; i < invocation.Arguments.Length; i++)
{
logParameters.Add(new LogParameter
{
Name = invocation.GetConcreteMethod().GetParameters()[i].Name,
Value = invocation.Arguments[i],
Type = invocation.Arguments[i].GetType().Name
});
}
var logDetail = new LogDetail
{
MethodName = invocation.Method.Name,
LogParameters = logParameters
};
return logDetail;
}
}
}