public override void After(AspectExecutedContext context) { base.After(context); _stopwatch.Stop(); string methodName = string.Format("{0}.{1}.{2}", context.Method.ReflectedType.Namespace, context.InvokeObject.GetType().Name, context.Method.Name ); double time = _stopwatch.Elapsed.TotalMilliseconds; if (time > _limitTime) { try { string message = $"{methodName} worked {time} ms (time)"; _logManager.Write(message, "performance info"); } catch (Exception e) { _exceptionManager.Handle(e); } } }
public override void After(AspectExecutedContext context) { base.After(context); string key = string.Format("{0}.{1}", context.Method.ReflectedType.Namespace, context.InvokeObject.GetType().Name ); _cacheManager.Delete(key); }
public override void After(AspectExecutedContext context) { Console.WriteLine("cache aspect çalıştı"); string methodName = string.Format("{0}.{1}", context.Method.ReflectedType.Namespace, context.InvokeObject.GetType().Name ); var key = string.Format("{0}", methodName); if (_cacheManager.IsExists(key)) { context.ReturnValue = _cacheManager.Get <object>(key); } else { object result = context.Method.Invoke(context.InvokeObject, context.args); context.ReturnValue = result; _cacheManager.Add(key, context.ReturnValue, _cacheDuration); } }
protected override object Invoke(MethodInfo targetMethod, object[] args) { try { AspectExecutingContext beforecontext = new AspectExecutingContext(); AspectExecutedContext aftercontext = new AspectExecutedContext(); beforecontext.Method = targetMethod; beforecontext.MethodName = targetMethod.Name; beforecontext.args = args; beforecontext.InvokeObject = _decorated; aftercontext.MethodName = targetMethod.Name; aftercontext.args = args; aftercontext.Method = targetMethod; aftercontext.InvokeObject = _decorated; Type instanceType = _decorated.GetType(); NebulaAspect[] typeAspects = (NebulaAspect[])(instanceType.GetCustomAttributes(typeof(NebulaAspect), true)); foreach (var aspect in typeAspects) { aspect.Before(beforecontext); } Type[] paramTypes = targetMethod.GetParameters().Select(v => v.ParameterType).ToArray(); NebulaAspect[] aspects = (NebulaAspect[])(instanceType.GetMethod(targetMethod.Name, paramTypes).GetCustomAttributes(typeof(NebulaAspect), true)); beforecontext.MethodName = targetMethod.Name; aftercontext.MethodName = targetMethod.Name; aftercontext.args = args; foreach (NebulaAspect aspect in aspects) { aspect.Before(beforecontext); } if (aftercontext.ReturnValue != null) { return(aftercontext.ReturnValue); } var result = targetMethod.Invoke(_decorated, args); foreach (var aspect in typeAspects) { aspect.After(aftercontext); } foreach (NebulaAspect aspect in aspects) { aspect.After(aftercontext); } return(result); } catch (Exception ex) when(ex is TargetInvocationException) { throw ex.InnerException ?? ex; } }