public IMethodInvocationResult Intercept(ISyncMethodInvocation invocation)
        {
            IMethodInvocationResult result = invocation.InvokeNext();

            if (result.Exception != null)
            {
                _logger.Error("The following exception is being logged: {Message}", result.Exception.Message);
            }

            return(result);
        }
Exemple #2
0
        public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            IMethodInvocationResult result = null;
            MethodInfo methodInfo          = methodInvocation.InstanceMethodInfo;
            Type       typeFoo             = methodInfo.ReturnType;

            result = methodInvocation.InvokeNext();
            stopwatch.Stop();

            Dictionary <string, string> requestDictionary = new Dictionary <string, string>();

            foreach (IArgument argument in methodInvocation.Arguments)
            {
                requestDictionary.Add(argument.ParameterInfo.Name, JsonConvert.SerializeObject(argument.Value, Formatting.Indented, new JsonSerializerSettings {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                }));
            }
            try
            {
                string request  = JsonConvert.SerializeObject(requestDictionary);
                string response = JsonConvert.SerializeObject(result.ReturnValue, Formatting.Indented, new JsonSerializerSettings {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
                _logger.LogInformation($"LogInterceptor logging process succesfully, method name : { methodInfo.DeclaringType.FullName}-{methodInfo.Name} | Request : {request} | Response : {response} | Total Processing Time  : {stopwatch.ElapsedMilliseconds}");
                PropertyInfo pi = result.ReturnValue.GetType().GetProperty("IsSuccess");

                bool isSuccess = (bool)(pi.GetValue(result.ReturnValue, null));
                if (isSuccess)
                {
                    // _logger.LogInformation($"LogInterceptor logging process succesfully, method name : { methodInfo.DeclaringType.FullName}-{methodInfo.Name} | Request : {request} | Response : {response} | Total Processing Time  : {stopwatch.ElapsedMilliseconds}");
                }
                else
                {
                    _logger.LogError($"LogInterceptor logging process succesfully, method name : { methodInfo.DeclaringType.FullName}-{methodInfo.Name} | Request : {request} | Response : {response} | Total Processing Time  : {stopwatch.ElapsedMilliseconds}");
                }
            }
            catch (Exception ex)
            {
            }
            return(result);
        }
Exemple #3
0
        public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation)
        {
            IMethodInvocationResult result = null;

            CacheAttribute attribute = methodInvocation.InstanceMethodInfo.GetCustomAttribute <CacheAttribute>();

            if (attribute != null)
            {
                string key  = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheName, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray());
                string area = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheArea, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray());

                CachedItemDTO cachedItem = CacheManager.GetValue(area, key);

                object cachedValue = null;

                if (cachedItem != null)
                {
                    cachedValue = cachedItem.CacheValue;
                }

                if (cachedValue != null)
                {
                    MethodInfo methodInfo = methodInvocation.InstanceMethodInfo;
                    Type       typeFoo    = methodInfo.ReturnType;

                    var returnData = Convert.ChangeType(cachedValue, typeFoo);

                    return(methodInvocation.CreateResult(returnData));
                }
                else
                {
                    result = methodInvocation.InvokeNext();

                    CacheManager.AddValue(area, key, result.ReturnValue, attribute.TimeoutSeconds);

                    return(result);
                }
            }
            else
            {
                result = methodInvocation.InvokeNext();
                return(result);
            }
        }
            public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                if (methodInvocation.InstanceMethodInfo.Name != "IncrementAsync")
                {
                    return(await methodInvocation.InvokeNextAsync());
                }

                if (_invoked)
                {
                    throw new InvalidOperationException("Interceptor should be invoked once only");
                }
                _invoked = true;

                IMethodInvocationResult result = null;

                for (var i = 0; i < _retries; i++)
                {
                    result = await methodInvocation.InvokeNextAsync().ConfigureAwait(false);

                    await Task.Delay(50).ConfigureAwait(false);
                }

                return(result);
            }
Exemple #5
0
 private static async Task <T> convertInvocationResultToGenericTask <T>(IMethodInvocationResult methodInvocationResult)
 {
     return((T)methodInvocationResult.GetReturnValueOrThrow());
 }
Exemple #6
0
 public static object ConvertInvocationResultToTask(Type resultType, IMethodInvocationResult methodInvocationResult)
 {
     return(callPrivateStaticGenericMethod(typeof(ReflectionHelper), "convertInvocationResultToGenericTask", resultType,
                                           methodInvocationResult));
 }
Exemple #7
0
 public static async Task ConvertInvocationResultToTask(IMethodInvocationResult methodInvocationResult)
 {
     methodInvocationResult.GetReturnValueOrThrow();
 }