Exemplo n.º 1
0
        public void Intercept(IInvocation invocation)
        {
            TimeMeasure measure = (_onBeforeExecution != null || _onAfterExecution != null)
            ? new TimeMeasure()
            : null;
            Exception error = null;

            _onBeforeExecution?.Invoke(_log, invocation.Method, invocation.Arguments);

            try
            {
                invocation.Proceed();
            }
            catch (Exception ex)
            {
                if (_logExceptions)
                {
                    _log.Trace("fatal error executing '{0}'", invocation.Method.Name, ex);
                }

                error = ex;

                throw;
            }
            finally
            {
                if (_onAfterExecution != null)
                {
                    long ticks = measure.ElapsedTicks;

                    _onAfterExecution(_log, invocation.Method, invocation.Arguments, invocation.ReturnValue, ticks, error);
                }

                if (measure != null)
                {
                    measure.Dispose();
                }
            }
        }