コード例 #1
0
        private void ExcuteExHandle(IInvocation invocation, Exception ex)
        {
            ExHandleAttribute attribute = AttributeHelper.GetAttribute <ExHandleAttribute>(invocation) as ExHandleAttribute;

            if (attribute == null)
            {
                if (ex != null)
                {
                    throw ex;
                }
                else
                {
                    return;
                }
            }
            if (attribute.IsLog)
            {
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("{0} {1} -----EXHandle ", "Exception", InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel, ex);
                }
            }
            if (attribute.IsThrow)
            {
                throw ex;
            }
            //若忽略则什么都不做
            if (attribute.IsIgnore)
            {
                return;
            }
        }
コード例 #2
0
        private void PostProceed(IInvocation invocation)
        {
            LogAttribute attribute = AttributeHelper.GetAttribute <LogAttribute>(invocation) as LogAttribute;

            if (attribute != null && attribute.IsPostLog)
            {
                //拦截器log屏蔽
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("{0} -----Post log", InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
            }
        }
コード例 #3
0
        private bool tryAccess(IInvocation invocation)
        {
            PermissionPointAttribute attribute = AttributeHelper.GetAttribute <PermissionPointAttribute>(invocation) as PermissionPointAttribute;

            if (attribute == null)
            {
                return(true);
            }
            string strLogHeader = "Access accepted";

            try
            {
                IPermissionPointResolve resolve = IocCoreFactory.Get <IPermissionPointResolve>(attribute.ResolveType);
                PermissionPoint         point   = new DefaultPermissionPoint(attribute,
                                                                             InterceptorHelper.GetInvocationTarget(invocation),
                                                                             InterceptorHelper.GetInvocationMethod(invocation) as MemberInfo,
                                                                             InterceptorHelper.GetInvocationMethodArgs(invocation)) as PermissionPoint;
                PermissionInfo info = resolve.Resolve(point);
                info++;
                if (attribute.IsAcceptLog && LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("{0} {1} {2} {3}-----Access Log ", strLogHeader, PrincipalTokenHolder.CurrentPrincipal.ToString(), attribute.ToString(), InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
            }
            catch (AccessException ex)
            {
                strLogHeader = "Access Denied";
                if (attribute.IsAcceptLog && LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("{0} {1} {2} {3}-----Access Log ", strLogHeader, PrincipalTokenHolder.CurrentPrincipal.ToString(), attribute.ToString(), InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel, ex);
                }
                if (attribute.IsAlert)
                {
                    Console.WriteLine("Access diny alert!");
                }
                if (attribute.IsThrow)
                {
                    throw ex;
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #4
0
ファイル: CacheInterceptor.cs プロジェクト: lyllylyq/IocCore
        public override void Intercept(IInvocation invocation)
        {
            CacheAttribute attribute = AttributeHelper.GetAttribute <CacheAttribute>(invocation) as CacheAttribute;

            attribute.Key = getKey(invocation, attribute);
            ICache cache     = IocCoreFactory.Get <ICache>();
            object cacheData = cache.Get(attribute.Key);

            if (cacheData != null)
            {
                invocation.ReturnValue = cacheData;
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("Cache Hit! Key:\"{0}\" Caller:{1}", attribute.Key, InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
                return;
            }
            IDependencyWrapper dependency;

            if (!String.IsNullOrWhiteSpace(attribute.DependencyCallback))
            {
                dependency = getDependency(invocation, attribute);
            }
            else
            {
                dependency = null;
            }
            invocation.Proceed();
            if (dependency != null)
            {
                object     returnValue       = invocation.ReturnValue;
                MethodInfo onRemovedCallback = invocation.TargetType.GetMethod(attribute.OnRemovedCallback);
                cache.Insert(
                    attribute.Key,
                    returnValue,
                    dependency.Instance,
                    attribute.Absolute,
                    attribute.Sliding,
                    (int)attribute.Priority,
                    onRemovedCallback);
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("Cache Insert! Key:\"{0}\" Caller:{1}", attribute.Key, InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
            }
        }