Пример #1
0
		protected override void BeforeCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			IDictionary cache = GetCache(info);

			lock (cache.SyncRoot)
			{
				CompoundValue   key  = GetKey(info);
				CacheAspectItem item = GetItem(cache, key);

				if (item != null && !item.IsExpired)
				{
					info.InterceptResult = InterceptResult.Return;
					info.ReturnValue     = item.ReturnValue;

					if (item.RefValues != null)
					{
						ParameterInfo[] pis = info.CallMethodInfo.Parameters;
						int             n   = 0;

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

					info.Cached = true;
				}
				else 
				{
					info.Items["CacheKey"] = key;
				}
			}
		}
Пример #2
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = Cache;

            lock (cache.SyncRoot)
            {
                _object = info.Object; // BVChanges: adding _object field

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

                if (key == null)
                {
                    return;
                }

                int  maxCacheTime = _instanceMaxCacheTime ?? MaxCacheTime;
                bool isWeak       = _instanceIsWeak ?? IsWeak;

                CacheAspectItem item = new CacheAspectItem();

                item.ReturnValue  = info.ReturnValue;
                item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
                                    DateTime.MaxValue:
                                    DateTime.Now.AddMilliseconds(maxCacheTime);

                ParameterInfo[] pis = info.CallMethodInfo.Parameters;

                int n = 0;

                foreach (ParameterInfo pi in pis)
                {
                    if (pi.ParameterType.IsByRef)
                    {
                        n++;
                    }
                }

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

                    n = 0;

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

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }
Пример #3
0
        public virtual void UnregisterCall(InterceptCallInfo info)
        {
            AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);

            lock (_currentCalls.SyncRoot)
                _currentCalls.Remove(info);
        }
Пример #4
0
        private static ConfigParameters GetConfigParameters(InterceptCallInfo info)
        {
            ConfigParameters cp = info.CallMethodInfo.LogParameters;

            if (cp == null)
            {
                info.CallMethodInfo.LogParameters = cp = new ConfigParameters();

                string[] ps = info.ConfigString.Split(';');

                foreach (string p in ps)
                {
                    string[] vs = p.Split('=');

                    if (vs.Length == 2)
                    {
                        switch (vs[0].ToLower().Trim())
                        {
                        case "filename":      cp.FileName = vs[1].Trim();  break;

                        case "mincalltime":   cp.MinCallTime = int.Parse(vs[1].Trim()); break;

                        case "logexceptions": cp.LogExceptions = bool.Parse(vs[1].Trim()); break;

                        case "logparameters": cp.LogParameters = bool.Parse(vs[1].Trim()); break;
                        }
                    }
                }
            }

            return(cp);
        }
Пример #5
0
        private static ConfigParameters GetConfigParameters(InterceptCallInfo info)
        {
            ConfigParameters cp = info.CallMethodInfo.CacheParameters;

            if (cp == null)
            {
                info.CallMethodInfo.CacheParameters = cp = new ConfigParameters();

                string[] ps = info.ConfigString.Split(';');

                foreach (string p in ps)
                {
                    string[] vs = p.Split('=');

                    if (vs.Length == 2)
                    {
                        switch (vs[0].ToLower().Trim())
                        {
                        case "maxcachetime": cp.MaxCacheTime = int.Parse(vs[1].Trim()); break;

                        case "isweak":       cp.IsWeak = bool.Parse(vs[1].Trim()); break;
                        }
                    }
                }
            }

            return(cp);
        }
Пример #6
0
		private static ConfigParameters GetConfigParameters(InterceptCallInfo info)
		{
			ConfigParameters cp = info.CallMethodInfo.LogParameters;

			if (cp == null)
			{
				info.CallMethodInfo.LogParameters = cp = new ConfigParameters();

				string[] ps = info.ConfigString.Split(';');

				foreach (string p in ps)
				{
					string[] vs = p.Split('=');

					if (vs.Length == 2)
					{
						switch (vs[0].ToLower().Trim())
						{
							case "filename":      cp.FileName      =            vs[1].Trim();  break;
							case "mincalltime":   cp.MinCallTime   = int. Parse(vs[1].Trim()); break;
							case "logexceptions": cp.LogExceptions = bool.Parse(vs[1].Trim()); break;
							case "logparameters": cp.LogParameters = bool.Parse(vs[1].Trim()); break;
						}
					}
				}
			}

			return cp;
		}
Пример #7
0
 protected override void OnFinally(InterceptCallInfo info)
 {
     if (IsEnabled)
     {
         LogOperation(info);
     }
 }
Пример #8
0
		protected override void BeforeCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			var cache = Cache;

			lock (cache.SyncRoot)
			{
				var key  = GetKey(info);
				var item = GetItem(cache, key);

				if (item != null && !item.IsExpired)
				{
					info.InterceptResult = InterceptResult.Return;
					info.ReturnValue     = item.ReturnValue;

					if (item.RefValues != 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;
				}
				else 
				{
					info.Items["CacheKey"] = key;
				}
			}
		}
Пример #9
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;

            lock (cache.SyncRoot)
            {
                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),
                };

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

                foreach (var pi in pis)
                {
                    if (pi.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)
                        {
                            item.RefValues[n++] = info.ParameterValues[i];
                        }
                    }
                }

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }
Пример #10
0
		protected override void BeforeCall(InterceptCallInfo info)
		{
			if (!IsEnabled || Thread.GetData(_counterSlot) != null)
				return;

			_counter.RegisterCall(info);

			Thread.SetData(_counterSlot, _counter);
		}
Пример #11
0
		public 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;
			}
		}
Пример #12
0
		protected override void AfterCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			IDictionary cache = GetCache(info);

			lock (cache.SyncRoot)
			{
				CompoundValue key = (CompoundValue)info.Items["CacheKey"];

				if (key == null)
					return;

				int  maxCacheTime = MaxCacheTime;
				bool isWeak       = IsWeak;

				if (!string.IsNullOrEmpty(info.ConfigString))
				{
					ConfigParameters cp = GetConfigParameters(info);

					if (cp.MaxCacheTime != null) maxCacheTime = (int) cp.MaxCacheTime;
					if (cp.IsWeak       != null) isWeak       = (bool)cp.IsWeak;
				}

				CacheAspectItem item = new CacheAspectItem();

				item.ReturnValue  = info.ReturnValue;
				item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
					DateTime.MaxValue:
					DateTime.Now.AddMilliseconds(maxCacheTime);

				ParameterInfo[] pis = info.CallMethodInfo.Parameters;

				int n = 0;

				foreach (ParameterInfo pi in pis)
					if (pi.ParameterType.IsByRef)
						n++;

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

					n = 0;

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

				cache[key] = isWeak? (object)new WeakReference(item): item;
			}
		}
Пример #13
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled || Thread.GetData(_counterSlot) != null)
            {
                return;
            }

            _counter.RegisterCall(info);

            Thread.SetData(_counterSlot, _counter);
        }
Пример #14
0
		protected override void BeforeCall(InterceptCallInfo info)
		{
			if (!IsEnabled || Thread.GetData(counterSlot) != null)
				return;

			MethodCallCounter counter = GetCounter(info);

			lock (counter.CurrentCalls.SyncRoot)
				counter.CurrentCalls.Add(info);

			Thread.SetData(counterSlot, counter);
		}
Пример #15
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);
			}
		}
Пример #16
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);
            }
        }
Пример #17
0
        public 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;
            }
        }
Пример #18
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled || Thread.GetData(counterSlot) != null)
            {
                return;
            }

            MethodCallCounter counter = GetCounter(info);

            lock (counter.CurrentCalls.SyncRoot)
                counter.CurrentCalls.Add(info);

            Thread.SetData(counterSlot, counter);
        }
Пример #19
0
		protected override void OnFinally(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

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

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

				Thread.SetData(_counterSlot, null);
			}
		}
Пример #20
0
        protected override void OnFinally(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

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

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

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

            if (info.Exception != null && parameters.LogExceptions ||
                info.Exception == null && time >= parameters.MinCallTime)
            {
                string callParameters = null;
                int    plen           = info.ParameterValues.Length;

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

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

                    callParameters = sb.ToString();
                }

                string exText = null;

                if (info.Exception != null)
                {
                    exText = string.Format(
                        " with exception '{0}' - \"{1}\"",
                        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);
            }
        }
Пример #22
0
		protected override void OnFinally(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			MethodCallCounter counter = GetCounter(info);
			MethodCallCounter prev    = (MethodCallCounter)Thread.GetData(counterSlot);

			if (counter == prev)
			{
				counter.AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);

				lock (counter.CurrentCalls.SyncRoot)
					counter.CurrentCalls.Remove(info);

				Thread.SetData(counterSlot, null);
			}
		}
Пример #23
0
		public void Test()
		{
			try
			{
				var info = new InterceptCallInfo();

				info.ParameterValues = new object[2];
				info.ParameterValues[0] = "123";
				info.ParameterValues[1] = 123;

				throw new ApplicationException();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
				Console.WriteLine(ex);
				throw;
			}
		}
Пример #24
0
        protected override void OnFinally(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            MethodCallCounter counter = GetCounter(info);
            MethodCallCounter prev    = (MethodCallCounter)Thread.GetData(counterSlot);

            if (counter == prev)
            {
                counter.AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);

                lock (counter.CurrentCalls.SyncRoot)
                    counter.CurrentCalls.Remove(info);

                Thread.SetData(counterSlot, null);
            }
        }
Пример #25
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = GetCache(info);

            lock (cache.SyncRoot)
            {
                CompoundValue   key  = GetKey(info);
                CacheAspectItem item = GetItem(cache, key);

                if (item != null && !item.IsExpired)
                {
                    info.InterceptResult = InterceptResult.Return;
                    info.ReturnValue     = item.ReturnValue;

                    if (item.RefValues != null)
                    {
                        ParameterInfo[] pis = info.CallMethodInfo.Parameters;
                        int             n   = 0;

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

                    info.Cached = true;
                }
                else
                {
                    info.Items["CacheKey"] = key;
                }
            }
        }
Пример #26
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;

            lock (cache.SyncRoot)
            {
                var key  = GetKey(info);
                var item = GetItem(cache, key);

                if (item != null && !item.IsExpired)
                {
                    info.InterceptResult = InterceptResult.Return;
                    info.ReturnValue     = item.ReturnValue;

                    if (item.RefValues != 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;
                }
                else
                {
                    info.Items["CacheKey"] = key;
                }
            }
        }
Пример #27
0
        private static void SetReturnFromCache(InterceptCallInfo info, CacheAspectItem item)
        {
            info.InterceptResult = InterceptResult.Return;
            info.ReturnValue     = item.ReturnValue;

            if (item.RefValues != 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;
        }
Пример #28
0
        protected override void BeforeCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            var cache = Cache;
            var key   = GetKey(info);
            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;
            }
        }
Пример #29
0
        private static MethodCallCounter GetCounter(InterceptCallInfo info)
        {
            if (info.CallMethodInfo.Counter == null)
            {
                lock (_counters.SyncRoot)
                    if (info.CallMethodInfo.Counter == null)
                    {
                        foreach (MethodCallCounter c in _counters)
                        {
                            if (c.InterceptorID == info.InterceptorID)
                            {
                                return(info.CallMethodInfo.Counter = c);
                            }
                        }

                        _counters.Add(info.CallMethodInfo.Counter = CreateCounter(info.CallMethodInfo));

                        info.CallMethodInfo.Counter.InterceptorID = info.InterceptorID;
                    }
            }

            return(info.CallMethodInfo.Counter);
        }
Пример #30
0
        // BVChanges: static -> virtual
        protected /*static*/ virtual CompoundValue GetKey(InterceptCallInfo info)
        {
            ParameterInfo[] parInfo     = info.CallMethodInfo.Parameters;
            object[]        parValues   = info.ParameterValues;
            object[]        keyValues   = new object[parValues.Length];
            bool[]          cacheParams = info.CallMethodInfo.CacheableParameters;

            if (cacheParams == null)
            {
                info.CallMethodInfo.CacheableParameters = cacheParams = new bool[parInfo.Length];

                for (int i = 0; i < parInfo.Length; i++)
                {
                    cacheParams[i] = IsCacheableParameterType(parInfo[i].ParameterType);
                }
            }

            for (int i = 0; i < parValues.Length; i++)
            {
                keyValues[i] = cacheParams[i] ? parValues[i] : null;
            }

            return(new CompoundValue(keyValues));
        }
Пример #31
0
        protected static CompoundValue GetKey(InterceptCallInfo info)
        {
            var parInfo     = info.CallMethodInfo.Parameters;
            var parValues   = info.ParameterValues;
            var keyValues   = new object[parValues.Length];
            var cacheParams = info.CallMethodInfo.CacheableParameters;

            if (cacheParams == null)
            {
                info.CallMethodInfo.CacheableParameters = cacheParams = new bool[parInfo.Length];

                for (var i = 0; i < parInfo.Length; i++)
                {
                    cacheParams[i] = IsCacheableParameterType(parInfo[i].ParameterType);
                }
            }

            for (var i = 0; i < parValues.Length; i++)
            {
                keyValues[i] = cacheParams[i] ? parValues[i] : null;
            }

            return(new CompoundValue(keyValues));
        }
Пример #32
0
 public virtual void RegisterCall(InterceptCallInfo info)
 {
     lock (_currentCalls.SyncRoot)
         _currentCalls.Add(info);
 }
Пример #33
0
		protected override void OnFinally(InterceptCallInfo info)
		{
			if (IsEnabled)
				LogOperation(info);
		}
Пример #34
0
 protected virtual CacheAspectItem CreateCacheItem(InterceptCallInfo info)
 {
     return(new CacheAspectItem());
 }
Пример #35
0
 protected override void BeforeCall(InterceptCallInfo info)
 {
     info.Items["ReturnValue"] = info.ReturnValue;
 }
Пример #36
0
		protected virtual CacheAspectItem CreateCacheItem(InterceptCallInfo info)
		{
			return new CacheAspectItem();
		}
Пример #37
0
		protected override void OnFinally(InterceptCallInfo info)
		{
			EndLock();
			base.OnFinally(info);
		}
Пример #38
0
		private static void LogOperationInternal(InterceptCallInfo info, Parameters parameters)
		{
			DateTime end  = DateTime.Now;
			int      time = (int)((end - info.BeginCallTime).TotalMilliseconds);

			if (info.Exception != null && parameters.LogExceptions ||
				info.Exception == null && time >= parameters.MinCallTime)
			{
				string callParameters = null;
				int    plen           = info.ParameterValues.Length;

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

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

					callParameters = sb.ToString();
				}

				string exText = null;

				if (info.Exception != null)
					exText = string.Format(
						" with exception '{0}' - \"{1}\"",
						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);
			}
		}
Пример #39
0
		private static MethodCallCounter GetCounter(InterceptCallInfo info)
		{
			if (info.CallMethodInfo.Counter == null)
				lock (_counters.SyncRoot)
					if (info.CallMethodInfo.Counter == null)
					{
						foreach (MethodCallCounter c in _counters)
							if (c.InterceptorID == info.InterceptorID)
								return info.CallMethodInfo.Counter = c;

						_counters.Add(info.CallMethodInfo.Counter = CreateCounter(info.CallMethodInfo));

						info.CallMethodInfo.Counter.InterceptorID = info.InterceptorID;
					}

			return info.CallMethodInfo.Counter;
		}
Пример #40
0
 protected virtual void OnFinally(InterceptCallInfo info)
 {
 }
Пример #41
0
		protected override void AfterCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			var cache = Cache;

			lock (cache.SyncRoot)
			{
				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),
				};

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

				foreach (var pi in pis)
					if (pi.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)
							item.RefValues[n++] = info.ParameterValues[i];
				}

				cache[key] = isWeak? (object)new WeakReference(item): item;
			}
		}
Пример #42
0
		public virtual void UnregisterCall(InterceptCallInfo info)
		{
			AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);

			lock (_currentCalls.SyncRoot)
				_currentCalls.Remove(info);
		}
Пример #43
0
		protected static CompoundValue GetKey(InterceptCallInfo info)
		{
			var parInfo     = info.CallMethodInfo.Parameters;
			var parValues   = info.ParameterValues;
			var keyValues   = new object[parValues.Length];
			var cacheParams = info.CallMethodInfo.CacheableParameters;

			if (cacheParams == null)
			{
				info.CallMethodInfo.CacheableParameters = cacheParams = new bool[parInfo.Length];

				for (var i = 0; i < parInfo.Length; i++)
					cacheParams[i] = IsCacheableParameterType(parInfo[i].ParameterType);
			}

			for (var i = 0; i < parValues.Length; i++)
				keyValues[i] = cacheParams[i] ? parValues[i] : null;

			return new CompoundValue(keyValues);
		}
Пример #44
0
		public virtual void RegisterCall(InterceptCallInfo info)
		{
			lock (_currentCalls.SyncRoot)
				_currentCalls.Add(info);
		}
Пример #45
0
		protected virtual IDictionary GetCache(InterceptCallInfo info)
		{
			return info.CallMethodInfo.MethodCallCache;
		}
Пример #46
0
		private static void SetReturnFromCache(InterceptCallInfo info, CacheAspectItem item)
		{
			info.InterceptResult = InterceptResult.Return;
			info.ReturnValue     = item.ReturnValue;

			if (item.RefValues != 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;
		}
Пример #47
0
		protected override void BeforeCall(InterceptCallInfo info)
		{
			if (!IsEnabled)
				return;

			var cache = Cache;
			var key   = GetKey(info);
			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;
			}
		}
Пример #48
0
 protected override void AfterCall(InterceptCallInfo info)
 {
     info.ReturnValue = (int)info.ReturnValue + (int)info.Items["ReturnValue"];
 }
Пример #49
0
		private static void LogOperationInternal(InterceptCallInfo info)
		{
			string   fileName      = FileName;
			int      minCallTime   = MinCallTime;
			bool     logExceptions = LogExceptions;
			bool     logParameters = LogParameters;

			if (!string.IsNullOrEmpty(info.ConfigString))
			{
				ConfigParameters cp = GetConfigParameters(info);

				if (cp.FileName      != null) fileName      =       cp.FileName.ToString();
				if (cp.MinCallTime   != null) minCallTime   = (int) cp.MinCallTime;
				if (cp.LogExceptions != null) logExceptions = (bool)cp.LogExceptions;
				if (cp.LogParameters != null) logParameters = (bool)cp.LogParameters;
			}

			DateTime end  = DateTime.Now;
			int      time = (int)((end - info.BeginCallTime).TotalMilliseconds);

			if (info.Exception != null && logExceptions ||
				info.Exception == null && time >= minCallTime)
			{
				string parameters = null;
				int    plen       = info.ParameterValues.Length;

				if (logParameters && plen > 0)
				{
					string[] pvs    = new string[plen];
					object[] values = info.ParameterValues;

					for (int i = 0; i < plen; i++)
					{
						object o = values[i];
						pvs[i] =
							o == null?   "<null>":
							o is string? "\"" + o + "\"":
							o is char?   "'"  + o + "'":
							o.ToString();
					}

					parameters = string.Join(", ", pvs);
				}

				string exText = null;

				if (info.Exception != null)
					exText = string.Format(
						" with exception '{0}' - \"{1}\"",
						info.Exception.GetType().FullName,
						info.Exception.Message);

				LogOutput(
					string.Format("{0}: {1}.{2}({3}) - {4} ms{5}.",
						end,
						info.CallMethodInfo.MethodInfo.DeclaringType.FullName,
						info.CallMethodInfo.MethodInfo.Name,
						parameters,
						time,
						exText),
					fileName);
			}
		}
Пример #50
0
		protected virtual void OnFinally (InterceptCallInfo info) {}
Пример #51
0
		private static ConfigParameters GetConfigParameters(InterceptCallInfo info)
		{
			ConfigParameters cp = info.CallMethodInfo.CacheParameters;

			if (cp == null)
			{
				info.CallMethodInfo.CacheParameters = cp = new ConfigParameters();

				string[] ps = info.ConfigString.Split(';');

				foreach (string p in ps)
				{
					string[] vs = p.Split('=');

					if (vs.Length == 2)
					{
						switch (vs[0].ToLower().Trim())
						{
							case "maxcachetime": cp.MaxCacheTime = int. Parse(vs[1].Trim()); break;
							case "isweak":       cp.IsWeak       = bool.Parse(vs[1].Trim()); break;
						}
					}
				}
			}

			return cp;
		}
Пример #52
0
		protected virtual void AfterCall (InterceptCallInfo info) {}
Пример #53
0
 protected virtual void BeforeCall(InterceptCallInfo info)
 {
 }
Пример #54
0
 protected virtual void AfterCall(InterceptCallInfo info)
 {
 }
Пример #55
0
        private static void LogOperationInternal(InterceptCallInfo info)
        {
            string fileName      = FileName;
            int    minCallTime   = MinCallTime;
            bool   logExceptions = LogExceptions;
            bool   logParameters = LogParameters;

            if (!string.IsNullOrEmpty(info.ConfigString))
            {
                ConfigParameters cp = GetConfigParameters(info);

                if (cp.FileName != null)
                {
                    fileName = cp.FileName.ToString();
                }
                if (cp.MinCallTime != null)
                {
                    minCallTime = (int)cp.MinCallTime;
                }
                if (cp.LogExceptions != null)
                {
                    logExceptions = (bool)cp.LogExceptions;
                }
                if (cp.LogParameters != null)
                {
                    logParameters = (bool)cp.LogParameters;
                }
            }

            DateTime end  = DateTime.Now;
            int      time = (int)((end - info.BeginCallTime).TotalMilliseconds);

            if (info.Exception != null && logExceptions ||
                info.Exception == null && time >= minCallTime)
            {
                string parameters = null;
                int    plen       = info.ParameterValues.Length;

                if (logParameters && plen > 0)
                {
                    string[] pvs    = new string[plen];
                    object[] values = info.ParameterValues;

                    for (int i = 0; i < plen; i++)
                    {
                        object o = values[i];
                        pvs[i] =
                            o == null?   "<null>":
                            o is string? "\"" + o + "\"" :
                            o is char?   "'" + o + "'" :
                            o.ToString();
                    }

                    parameters = string.Join(", ", pvs);
                }

                string exText = null;

                if (info.Exception != null)
                {
                    exText = string.Format(
                        " with exception '{0}' - \"{1}\"",
                        info.Exception.GetType().FullName,
                        info.Exception.Message);
                }

                LogOutput(
                    string.Format("{0}: {1}.{2}({3}) - {4} ms{5}.",
                                  end,
                                  info.CallMethodInfo.MethodInfo.DeclaringType.FullName,
                                  info.CallMethodInfo.MethodInfo.Name,
                                  parameters,
                                  time,
                                  exText),
                    fileName);
            }
        }
Пример #56
0
 protected virtual void OnCatch(InterceptCallInfo info)
 {
 }
Пример #57
0
		protected virtual void OnCatch   (InterceptCallInfo info) {}
Пример #58
0
 protected virtual IDictionary GetCache(InterceptCallInfo info)
 {
     return(info.CallMethodInfo.MethodCallCache);
 }
Пример #59
0
 protected override void OnFinally(InterceptCallInfo info)
 {
     EndLock();
     base.OnFinally(info);
 }
Пример #60
0
        protected override void AfterCall(InterceptCallInfo info)
        {
            if (!IsEnabled)
            {
                return;
            }

            IDictionary cache = GetCache(info);

            lock (cache.SyncRoot)
            {
                CompoundValue key = (CompoundValue)info.Items["CacheKey"];

                if (key == null)
                {
                    return;
                }

                int  maxCacheTime = MaxCacheTime;
                bool isWeak       = IsWeak;

                if (!string.IsNullOrEmpty(info.ConfigString))
                {
                    ConfigParameters cp = GetConfigParameters(info);

                    if (cp.MaxCacheTime != null)
                    {
                        maxCacheTime = (int)cp.MaxCacheTime;
                    }
                    if (cp.IsWeak != null)
                    {
                        isWeak = (bool)cp.IsWeak;
                    }
                }

                CacheAspectItem item = new CacheAspectItem();

                item.ReturnValue  = info.ReturnValue;
                item.MaxCacheTime = maxCacheTime == int.MaxValue || maxCacheTime < 0?
                                    DateTime.MaxValue:
                                    DateTime.Now.AddMilliseconds(maxCacheTime);

                ParameterInfo[] pis = info.CallMethodInfo.Parameters;

                int n = 0;

                foreach (ParameterInfo pi in pis)
                {
                    if (pi.ParameterType.IsByRef)
                    {
                        n++;
                    }
                }

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

                    n = 0;

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

                cache[key] = isWeak? (object)new WeakReference(item): item;
            }
        }