public void OnException(ExceptionMethodArgs exceptionMethodArgs) { ParameterInfo[] parameterInfos = exceptionMethodArgs.MethodInfo.GetParameters(); List <LogDetailParameter> logParameters = new List <LogDetailParameter>(); for (int i = 0; i < exceptionMethodArgs.Arguments.Length; i++) { if (exceptionMethodArgs.Arguments[i] != null) { var parameter = exceptionMethodArgs.Arguments[i].GetType().BaseType.BaseType == typeof(System.Linq.Expressions.LambdaExpression) ? exceptionMethodArgs.Arguments[i].ToString() : exceptionMethodArgs.Arguments[i]; logParameters.Add(new LogDetailParameter { Name = parameterInfos[i].Name, Type = parameterInfos[i].ParameterType.Name, Value = parameter }); } else { logParameters.Add(new LogDetailParameter { Name = parameterInfos[i].Name, Type = parameterInfos[i].ParameterType.Name, Value = null }); } } LogDetail logDetail = new LogDetail { MethodCallDate = DateTime.Now, ClassName = exceptionMethodArgs.MethodInfo.DeclaringType.FullName, MethodName = exceptionMethodArgs.MethodInfo.Name, Parameters = logParameters, Message = exceptionMethodArgs.Exception.Message, InnerException = exceptionMethodArgs.Exception.InnerException == null ? "" : exceptionMethodArgs.Exception.InnerException.Message, StackStrace = exceptionMethodArgs.Exception.StackTrace }; var jsonResult = JsonConvert.SerializeObject(logDetail, Formatting.Indented); LogMethodParameter logMethodParameter = new LogMethodParameter() { LogName = $"{exceptionMethodArgs.MethodInfo.Name}_err", Message = jsonResult }; _logger.Log(logMethodParameter); }
private void RunOnExceptionInterception(object[] aspects, ExceptionMethodArgs exceptionMethodArgs) { if (aspects == null) { return; } foreach (IInterception loopAttribute in aspects) { if (loopAttribute is IExceptionInterception) { ((IExceptionInterception)loopAttribute).OnException(exceptionMethodArgs); } } }
protected override object Invoke(MethodInfo targetMethod, object[] args) { object[] interceptions = null; InterceptionArgs interceptionArgs = null; try { interceptionArgs = CreateAspectArgs(targetMethod, args); interceptions = GetInterceptions(targetMethod); // BeforeMethodArgs çalıştırılır. Cache vb. gelen datalar result değişkenine aktarılır BeforeMethodArgs beforeMethodArgs = new BeforeMethodArgs(interceptionArgs); object result = RunOnBeforeInterception(interceptions, beforeMethodArgs); //result dolu ise ilgili methodun çalışmasına gerek yoktur if (result == null) { //result null ise ilgili method çalıştırılır. result = targetMethod.Invoke(_service, args); } //After İnterceptionlar çalıştırılır AfterMethodArgs afterMethodArgs = new AfterMethodArgs(interceptionArgs); RunOnAfterInterception(interceptions, new AfterMethodArgs(afterMethodArgs, result)); //RunOnAfterInterception(interceptions, new AfterMethodArgs(interceptionArgs, result)); return(result); } catch (Exception ex) { var exArg = new ExceptionMethodArgs(interceptionArgs, ex); RunOnExceptionInterception(interceptions, exArg); throw ex.InnerException ?? ex; //return null; } //catch (Exception ex) when (ex is TargetInvocationException) //{ // var exArg = new ExceptionMethodArgs(e,ex); // RunOnExceptionInterception(interceptions, exArg); // //throw ex.InnerException ?? ex; // return null; //} }