private string CreateLogData(string methodStage, MethodExecutionArgs args) { var str = new StringBuilder(); str.AppendLine(); str.AppendLine(string.Format(methodStage + " {0} ", args.Method)); foreach (var argument in args.Arguments) { var argType = argument.GetType(); str.Append(argType.Name + ": "); if (argType == typeof(string) || argType.IsPrimitive) { str.Append(argument); } else { foreach (var property in argType.GetProperties()) { str.AppendFormat("{0} = {1}; ", property.Name, property.GetValue(argument, null)); } } } return str.ToString(); }
public override void OnSuccess(MethodExecutionArgs args) { for (int i = 0; i < args.Arguments.Count; i++) { if (args.Arguments[i] is int) args.Arguments[i] = -1; else if (args.Arguments[i] is string) args.Arguments[i] = "I changed your value"; } }
public BoundaryAspectGenerator(object instance, DynamicMetaObject metaObj, IEnumerable<OnMethodBoundaryAspect> aspects, MethodInfo method, IEnumerable<DynamicMetaObject> args, IEnumerable<Type> argsTypes) { _rule = metaObj.Restrictions; var methExecArgs = new MethodExecutionArgs(instance, method, new Arguments(args.Select(x => x.Value).ToArray())); _aspectCalls = new AspectCalls(metaObj.Expression, aspects, args, methExecArgs, argsTypes.Any(t => t.IsByRef)); }
public override void OnException(MethodExecutionArgs args) { var str = new StringBuilder(); str.AppendLine(); str.Append(args.Exception.Message); str.AppendLine(); if (args.Instance != null) { var instType = args.Instance.GetType(); str.AppendFormat("Type = {0}; ", instType.Name); foreach (var property in instType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { str.AppendFormat("{0} = {1}; ", property.Name, property.GetValue(args.Instance, null)); } } str.AppendLine(); Console.WriteLine(str.ToString()); args.FlowBehavior = FlowBehavior.RethrowException; }
/// <summary> /// When an exception is happened then this method will be called. /// This is a perfect place to log the error messages in an generic way and do something useful with that information. /// </summary> /// <param name="args">Method arguments including return value and all necessary info.</param> public virtual void OnException(MethodExecutionArgs args) { }
/// <summary> /// Method executed after the body of method to which this aspect is applied, /// but only when the method successfully returns. /// </summary> /// <param name="args">Method arguments including return value and all necessary info.</param> public virtual void OnSuccess(MethodExecutionArgs args) { }
/// <summary> /// Method executed after the body of method to which this aspect is applied /// (this method is invoked from the finally block, it's mean that this method will be invoked in any way). /// This is a good place where to log the return value, inform other applications that the method has finished the execution. /// </summary> /// <param name="args">Method arguments including return value and all necessary info.</param> public virtual void OnExit(MethodExecutionArgs args) { }
/// <summary> /// Method executed before the body of method to which this aspect is applied. /// OnEntry method is a perfect place where to implement the logic such as input parameters logging, caching, authentication/authorization. /// </summary> /// <param name="args">Method arguments including return value and all necessary info.</param> public virtual void OnEntry(MethodExecutionArgs args) { }
public override void OnSuccess(MethodExecutionArgs args) { var testEntity = (TestEntity)args.Arguments[0]; testEntity.Name = "KingAOP_OnSuccess"; testEntity.Number = 100; }
public override void OnSuccess(MethodExecutionArgs args) { args.ReturnValue = (int)args.ReturnValue + 1; }
public override void OnSuccess(MethodExecutionArgs args) { args.Arguments[0] = (int)args.Arguments[0] + 1; }
public override void OnEntry(MethodExecutionArgs args) { Console.WriteLine("OnEntry: Hello KingAOP"); }
public override void OnSuccess(MethodExecutionArgs args) { Console.WriteLine("OnSuccess: Hello KingAOP"); }
public override void OnEntry(MethodExecutionArgs args) { string logData = CreateLogData("Entering", args); Console.WriteLine(logData); }
public override void OnEntry(MethodExecutionArgs args) { var entity = (TestEntity)args.Arguments[0]; entity.Name = "ChangedName"; entity.Number = 999; }
public AspectCalls(Expression origMethod, IEnumerable<OnMethodBoundaryAspect> aspects, IEnumerable<DynamicMetaObject> args, MethodExecutionArgs methExecArgs, bool isByRefArgs) : this() { _origMethod = origMethod; _args = args; MethodExecutionArgs = methExecArgs; _isByRefArgs = isByRefArgs; ArgsExpression = Expression.Constant(methExecArgs); RetMethodValue = Expression.Parameter(typeof(object)); foreach (var aspect in aspects) { EntryCalls.Add(GenerateCall("OnEntry", aspect)); SuccessCalls.Add(GenerateCall("OnSuccess", aspect)); ExceptionCalls.Add(GenerateCall("OnException", aspect)); ExitCalls.Add(GenerateCall("OnExit", aspect)); } OriginalCall = GenerateOriginalCall(); }