Castle动态代理拦截

比如现在有一个方法,进行积分奖励

PointAdd

在不改变原来方法的基础上,增加积分奖励的日志

using Castle.DynamicProxy; 
public class AuditTraceInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var methodInfo = invocation.Method;
            if (methodInfo == null)
            {
                methodInfo = invocation.MethodInvocationTarget;
            }

            var trace = methodInfo.GetCustomAttributes<AuditTraceAttribute>(true).FirstOrDefault();

            if (trace != null)
            {
                var actionLog = new UserActionEntity
                {
                    ApplicationType = "MyApplication",
                    ProgramId = PageBase.CurrentProgramID,
                    UserId = BOPageBase.CurrentUser.ID,
                    ActionType = trace.ActionType,
                    SessionId = HttpContext.Current.Session.SessionID,
                    PageRelativeUrl = HttpContext.Current.Request.Url.PathAndQuery,
                    IpAddress = HttpContext.Current.Request.UserHostAddress
                };
                var requestURL = HttpContext.Current.Request.Path;
                var pageName = requestURL.Substring(requestURL.LastIndexOf("/") + 1, requestURL.Length - requestURL.LastIndexOf("/") - 1);
                actionLog.PageName = pageName;
                LogUtil.CreateLog(LogLevel.Message, $"Method {methodInfo.Name} has been Intercepted");
                foreach (var item in invocation.Arguments)
                {
                    LogUtil.CreateLog(LogLevel.Message,item.ToString());
                }
            }

            invocation.Proceed();

            LogUtil.CreateLog(LogLevel.Message, "Method has been Intercepted Done");

        }
    }

Castle~动态代理实现对方法的拦截

原文地址:https://www.cnblogs.com/chucklu/p/10883550.html