public void Intercept(IInvocation invocation) { MethodInfo methodInfo = invocation.MethodInvocationTarget; if (methodInfo == null) { methodInfo = invocation.Method; } //we take the settings from the first attribute we find searching method first //If there is at least one attribute, the call gets wrapped with an exception handler var assemblyAttributes = (ExceptionHandlerAttribute[]) methodInfo.ReflectedType.Assembly.GetCustomAttributes(typeof(ExceptionHandlerAttribute), false); var classAttributes = (ExceptionHandlerAttribute[]) methodInfo.ReflectedType.GetCustomAttributes(typeof(ExceptionHandlerAttribute), false); var methodAttributes = (ExceptionHandlerAttribute[])methodInfo.GetCustomAttributes(typeof(ExceptionHandlerAttribute), false); if (assemblyAttributes.Length == 0 && classAttributes.Length == 0 && methodAttributes.Length == 0) { invocation.Proceed(); } else { ExceptionHandlerAttributeSettings exceptionHandlerAttributeSettings = GetExceptionHandlerSettings(assemblyAttributes, classAttributes, methodAttributes); try { invocation.Proceed(); } catch (Exception err) { exceptionLogger.LogException(err, exceptionHandlerAttributeSettings.IsSilent, methodInfo.ReflectedType); if (exceptionHandlerAttributeSettings.IsSilent) { if (exceptionHandlerAttributeSettings.ExceptionType == null || exceptionHandlerAttributeSettings.ExceptionType == err.GetType()) { invocation.ReturnValue = exceptionHandlerAttributeSettings.ReturnValue; } else { throw; } } else { throw; } } } }
public ExceptionHandlerAttribute() { Settings = new ExceptionHandlerAttributeSettings(); }