public override void OnExit(PostSharp.Aspects.MethodExecutionArgs args) { Console.WriteLine(string.Format("[{0}] took {1} ms to execute", new StackTrace().GetFrame(1).GetMethod().Name, _StopWatch.ElapsedMilliseconds)); base.OnExit(args); }
/// <summary> /// 入口处理 /// </summary> /// <param name="args"></param> public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args) { //当为Write模式时,不从缓存读取数据.&& 当RemoveOnSuccess时不继续执行OnEntry if (g_ActionType == ActionType.Write) { return; } string key = GetMethodKey(args, this); try { object returnObj = RedisService.Get <Object>(g_configName, key); if (returnObj != null) { args.ReturnValue = returnObj; args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Return; return; } } catch (Exception ex) { } }
public override void OnException(PostSharp.Aspects.MethodExecutionArgs args) { log4net.LogManager.GetLogger("").Warn("EXCP-", args.Exception); #if DEBUG args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Default; #else args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Continue; #endif }
public override void OnException(PostSharp.Aspects.MethodExecutionArgs args) { StringBuilder sb = new StringBuilder(); sb.Append("*********************************************************************************************\r\n"); sb.Append("ErrorTime:" + System.DateTime.Now + "\r\n"); sb.Append("ErrorFun:" + args.Exception.Source + "\r\n"); sb.Append("ErrorDetail:" + args.Exception.StackTrace.ToString() + "," + args.Exception.Message + "\r\n"); sb.Append("*********************************************************************************************\r\n\r\n\r\n"); WriteFile(AppDomain.CurrentDomain.BaseDirectory + "\\SystemError.txt", sb.ToString()); base.OnException(args); //args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Return; }
private static string GetMethodKey(PostSharp.Aspects.MethodExecutionArgs args, CacheAttribute metadata) { string key; key = metadata.g_CacheKey.Trim(); #region 判断是否包含格式化 var match = Regex.Matches(key, "{\\d+}", RegexOptions.IgnoreCase | RegexOptions.Singleline); if (match.Count > 0) { try { key = string.Format(key, args.Arguments.ToArray()); } catch (Exception ex) { } } #endregion return(key); }
/// <summary> /// 方法执行后,置入缓存 /// </summary> /// <param name="args"></param> public override void OnSuccess(PostSharp.Aspects.MethodExecutionArgs args) { //当为Read模式时,只读取缓存数据,不进行写操作 .&& 当RemoveOnEntry操作时,删除缓存的操作只在OnEntry进行,退出OnSuccess. if (g_ActionType == ActionType.Read) { return; } try { string key = GetMethodKey(args, this); if (g_CacheSeconds > 0) { RedisService.Set <Object>(g_configName, key, args.ReturnValue, g_CacheSeconds); } else { RedisService.Set <Object>(g_configName, key, args.ReturnValue); } } catch (Exception ex) { } base.OnSuccess(args); }
public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args) { if (args.Arguments.Any()) { args.Arguments .ToList().ForEach( argument => { var locator = new HasSpecLocator(); var specs = typeof(HasSpecLocator) .GetMethod("Locate") .MakeGenericMethod(argument.GetType()) .Invoke(locator, BindingFlags.Public, null, null, null); foreach (var iHasSpec in ((IEnumerable)specs)) { var spec = typeof(IHasSpec <>) .MakeGenericType(argument.GetType()) .GetMethod("GetSpec") .Invoke(iHasSpec, new object[] { }); var list = Activator.CreateInstance(typeof(List <>).MakeGenericType(argument.GetType())); typeof(List <>).MakeGenericType(argument.GetType()) .GetMethod("Add") .Invoke(list, new object[] { argument }); var arr = typeof(List <>).MakeGenericType(argument.GetType()) .GetMethod("ToArray") .Invoke(list, new object[] { }); typeof(Spec <>) .MakeGenericType(argument.GetType()) .GetMethod("Run") .Invoke(spec, new object[] { arr }); } }); } else { var locator = new HasSpecLocator(); var specs = typeof(HasSpecLocator) .GetMethod("Locate") .MakeGenericMethod(args.Instance.GetType()) .Invoke(locator, BindingFlags.Public, null, null, null); foreach (var iHasSpec in ((IEnumerable)specs)) { var spec = typeof(IHasSpec <>) .MakeGenericType(args.Instance.GetType()) .GetMethod("GetSpec") .Invoke(iHasSpec, new object[] { }); var list = Activator.CreateInstance(typeof(List <>).MakeGenericType(args.Instance.GetType())); typeof(List <>).MakeGenericType(args.Instance.GetType()) .GetMethod("Add") .Invoke(list, new object[] { args.Instance }); var arr = typeof(List <>).MakeGenericType(args.Instance.GetType()) .GetMethod("ToArray") .Invoke(list, new object[] { }); typeof(Spec <>) .MakeGenericType(args.Instance.GetType()) .GetMethod("Run") .Invoke(spec, new object[] { arr }); } } }
/// <summary> /// 流程完成退出时 /// </summary> /// <param name="args"></param> public override void OnExit(PostSharp.Aspects.MethodExecutionArgs args) { base.OnExit(args); }
public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args) { _stopWatch = Stopwatch.StartNew(); base.OnEntry(args); }