zoukankan      html  css  js  c++  java
  • 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~动态代理实现对方法的拦截

  • 相关阅读:
    Linux安装telnet
    linux 打压缩包
    JAVA读写文件
    LINUX安装NGINX
    Linux安装JDK
    java判断字符串中是否包含中文 过滤中文
    操作MySQL
    Java序列化对象-字符串转换
    nginx
    linux操作命令
  • 原文地址:https://www.cnblogs.com/chucklu/p/10883550.html
Copyright © 2011-2022 走看看