Пример #1
0
        public virtual void UnregisterCall(InterceptCallInfo info)
        {
            AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);

            lock (CurrentCalls.SyncRoot)
                CurrentCalls.Remove(info);
        }
        static void SetReturnFromCache(InterceptCallInfo info, CacheAspectItem item)
        {
            Debug.Assert(item.ReturnValue != null, "item.ReturnValue != null");

            info.InterceptResult = InterceptResult.Return;
            info.ReturnValue     = item.ReturnValue;

            if (item.RefValues != null)
            {
                Debug.Assert(info.CallMethodInfo != null, "info.CallMethodInfo != null");
                Debug.Assert(info.ParameterValues != null, "info.ParameterValues != null");

                var pis = info.CallMethodInfo.Parameters;
                var n   = 0;

                for (var i = 0; i < pis.Length; i++)
                {
                    if (pis[i].ParameterType.IsByRef)
                    {
                        info.ParameterValues[i] = item.RefValues[n++];
                    }
                }
            }

            info.Cached = true;
        }
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;
            var key   = (CompoundValue)info.Items["CacheKey"];

            if (key == null)
            {
                return;
            }

            var maxCacheTime = _instanceMaxCacheTime ?? MaxCacheTime;
            var isWeak       = _instanceIsWeak ?? IsWeak;

            var item = new CacheAspectItem
            {
                ReturnValue  = info.ReturnValue,
                MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0
                                        ? DateTime.MaxValue
                                        : DateTime.Now.AddMilliseconds(maxCacheTime),
            };

            Debug.Assert(info.CallMethodInfo != null, "info.CallMethodInfo != null");

            var pis = info.CallMethodInfo.Parameters;
            var n   = 0;

            for (int i = 0; i < pis.Length; i++)
            {
                if (pis[i].ParameterType.IsByRef)
                {
                    n++;
                }
            }

            if (n > 0)
            {
                item.RefValues = new object[n];

                n = 0;

                for (var i = 0; i < pis.Length; i++)
                {
                    if (pis[i].ParameterType.IsByRef)
                    {
                        Debug.Assert(info.ParameterValues != null, "info.ParameterValues != null");
                        item.RefValues[n++] = info.ParameterValues[i];
                    }
                }
            }

            Debug.Assert(cache != null, nameof(cache) + " != null");
            cache[key] = isWeak ? (object)new WeakReference(item) : item;
        }
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled || Thread.GetData(_counterSlot) != null)
            {
                return;
            }

            _counter?.RegisterCall(info);

            Thread.SetData(_counterSlot, _counter);
        }
Пример #5
0
        protected override void OnFinally(InterceptCallInfo info)
        {
            if (IsEnabled)
            {
                _parameters.FileName      = _instanceFileName ?? FileName;
                _parameters.MinCallTime   = _instanceMinCallTime ?? MinCallTime;
                _parameters.LogExceptions = _instanceLogExceptions ?? LogExceptions;
                _parameters.LogParameters = _instanceLogParameters ?? LogParameters;

                LogOperation(info, _parameters);
            }
        }
Пример #6
0
        public virtual void Intercept(InterceptCallInfo info)
        {
            switch (info.InterceptType)
            {
            case InterceptType.BeforeCall: BeforeCall(info); break;

            case InterceptType.AfterCall:  AfterCall(info); break;

            case InterceptType.OnCatch:    OnCatch(info); break;

            case InterceptType.OnFinally:  OnFinally(info); break;
            }
        }
        protected override void OnFinally(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var prev = (MethodCallCounter)Thread.GetData(_counterSlot);

            if (_counter == prev)
            {
                _counter?.UnregisterCall(info);

                Thread.SetData(_counterSlot, null);
            }
        }
Пример #8
0
        static void LogOperationInternal(InterceptCallInfo info, Parameters parameters)
        {
            var end  = DateTime.Now;
            var time = (int)((end - info.BeginCallTime).TotalMilliseconds);

            if (info.Exception != null && parameters.LogExceptions ||
                info.Exception == null && time >= parameters.MinCallTime)
            {
                Debug.Assert(info.ParameterValues != null, "info.ParameterValues != null");

                string?callParameters = null;
                var    plen           = info.ParameterValues.Length;

                if (parameters.LogParameters && plen > 0)
                {
                    var sb     = new StringBuilder();
                    var values = info.ParameterValues;

                    FormatParameter(values[0], sb);

                    for (var i = 1; i < plen; i++)
                    {
                        FormatParameter(values[i], sb.Append(", "));
                    }

                    callParameters = sb.ToString();
                }

                string?exText = null;

                if (info.Exception != null)
                {
                    exText = $" with exception '{info.Exception.GetType().FullName}' - \"{info.Exception.Message}\"";
                }

                LogOutput(
                    string.Format("{0}: {1}.{2}({3}) - {4} ms{5}{6}.",
                                  end,
                                  info.CallMethodInfo?.MethodInfo.DeclaringType?.FullName,
                                  info.CallMethodInfo?.MethodInfo.Name,
                                  callParameters,
                                  time,
                                  info.Cached? " from cache": null,
                                  exText),
                    parameters.FileName);
            }
        }
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;
            var key   = GetKey(info);

            Debug.Assert(cache != null, nameof(cache) + " != null");

            var item = GetItem(cache, key);

            if (item != null && !item.IsExpired)
            {
                SetReturnFromCache(info, item);
                return;
            }

            try
            {
                Lock();

                item = GetItem(cache, key);

                if (item != null && !item.IsExpired)
                {
                    SetReturnFromCache(info, item);
                    EndLock();
                }
                else
                {
                    info.Items["CacheKey"] = key;
                }
            }
            catch
            {
                EndLock();
                throw;
            }
        }
 protected override void OnFinally(InterceptCallInfo info)
 {
     EndLock();
     base.OnFinally(info);
 }
Пример #11
0
 protected virtual void OnFinally(InterceptCallInfo info)
 {
 }
Пример #12
0
 protected virtual void OnCatch(InterceptCallInfo info)
 {
 }
Пример #13
0
 protected virtual void AfterCall(InterceptCallInfo info)
 {
 }
Пример #14
0
 protected virtual void BeforeCall(InterceptCallInfo info)
 {
 }
Пример #15
0
 public virtual void RegisterCall(InterceptCallInfo info)
 {
     lock (CurrentCalls.SyncRoot)
         CurrentCalls.Add(info);
 }