Пример #1
0
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            try
            {
                var argsDictionary = new Dictionary <string, object>();
                var args           = invocationContext.GetExecutingMethodInfo().GetParameters();
                for (var i = 0; i < args.Length; i++)
                {
                    var argumentValue = invocationContext.GetParameterValue(i);
                    var argumentName  = args[i].Name;
                    argsDictionary.Add(argumentName, argumentValue);
                }

                _cacheKey = invocationContext.GetExecutingMethodInfo().GetParameters().Length > 0
                    ? CacheKeyGenerator.GenerateCacheKey(invocationContext.GetExecutingMethodInfo(), argsDictionary)
                    : invocationContext.GetExecutingMethodInfo().Name;


                var doesCacheExist = _cacheProvider.Get(_cacheKey);

                if (doesCacheExist != null)
                {
                    invocationContext.OverrideMethodReturnValue(doesCacheExist);
                    invocationContext.BypassInvocation();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }
Пример #2
0
        public void AfterInvoke(InvocationContext invocationContext, object methodResult)
        {
            try
            {
                var endDate       = DateTime.Now;
                var executionTime = (int)((endDate - _startTime).TotalMilliseconds);

                var argsDictionary = new Dictionary <string, object>();
                var args           = invocationContext.GetExecutingMethodInfo().GetParameters();
                for (var i = 0; i < args.Length; i++)
                {
                    var argumentValue = invocationContext.GetParameterValue(i);
                    var argumentName  = args[i].Name;
                    argsDictionary.Add(argumentName, argumentValue);
                }

                var methodInfo = invocationContext.GetExecutingMethodInfo();

                var auditEntity = new Audit
                {
                    Id            = Guid.NewGuid(),
                    Input         = JsonConvert.SerializeObject(argsDictionary),
                    Output        = JsonConvert.SerializeObject(methodResult),
                    ClassName     = methodInfo.Module.ToString(),
                    MethodName    = methodInfo.Name,
                    StartTime     = _startTime,
                    EndTime       = endDate,
                    ExecutionTime = executionTime
                };

                _auditorProvider.Save(auditEntity);
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }
Пример #3
0
        public void AfterInvoke(InvocationContext invocationContext, object methodResult)
        {
            try
            {
                var attribute = invocationContext.GetAttributeFromMethod <CacheAttribute>();

                var argsDictionary = new Dictionary <string, object>();
                var args           = invocationContext.GetExecutingMethodInfo().GetParameters();
                for (var i = 0; i < args.Length; i++)
                {
                    var argumentValue = invocationContext.GetParameterValue(i);
                    var argumentName  = args[i].Name;
                    argsDictionary.Add(argumentName, argumentValue);
                }

                var cacheKey = invocationContext.GetExecutingMethodInfo().GetParameters().Length > 0
                    ? CacheKeyGenerator.GenerateCacheKey(invocationContext.GetExecutingMethodInfo(), argsDictionary)
                    : invocationContext.GetExecutingMethodInfo().Name;

                var cachingDuration = new TimeSpan(attribute.Day, attribute.Hour, attribute.Minute, attribute.Second);

                if (cachingDuration > TimeSpan.Zero)
                {
                    _cacheProvider.AddOrUpdate(cacheKey, methodResult, cachingDuration);
                }
                else
                {
                    _cacheProvider.AddOrUpdate(_cacheKey, methodResult, new TimeSpan(0, 10, 0));
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }
Пример #4
0
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this._logAttribute = invocationContext.GetAttributeFromMethod <LogAttribute>();

            if (typeof(ILoggerService).IsAssignableFrom(_logAttribute._loggerServiceType) == false)
            {
                throw new Exception("Wrong log manager");
            }

            logManager = (ILoggerService)ActivatorUtilities.CreateInstance(_serviceProvider, _logAttribute._loggerServiceType);

            LogDetail _log = new LogDetail()
            {
                DateTime   = DateTime.Now,
                FullName   = invocationContext.GetInvocation().TargetType.FullName,
                MethodName = invocationContext.GetExecutingMethodInfo().Name,
                Type       = _logAttribute._logType.ToString()
                             // Arguments = GetArguments(invocationContext)
            };

            logManager.Logging(_log);
        }