Exemplo n.º 1
0
        /// <summary>
        /// Adds logging to the method invocation.
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns> return value of the targetd method</returns>
        public object Invoke(IMethodInvocation invocation)
        {
            IASALog log = ASALogManager.GetLogger(invocation.TargetType);

            string        methodName = invocation.TargetType.ToString() + "." + invocation.Method.Name;
            StringBuilder arguments  = new StringBuilder();

            ParameterInfo[] parameterInfos = invocation.Method.GetParameters();
            object[]        argValues      = invocation.Arguments;
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                arguments.Append(parameterInfos[i].Name).Append("=").Append(argValues[i]);
                if (i < (parameterInfos.Length - 1))
                {
                    arguments.Append("; ");
                }
            }
            if (LogEntry)
            {
                log.LogMethodEntry(methodName, arguments.ToString());
            }

            object   returnValue          = null;
            bool     exitThroughException = false;
            DateTime startTime            = DateTime.Now;

            try
            {
                returnValue = invocation.Proceed();
                return(returnValue);
            }
            catch (Exception e)
            {
                if (logException)
                {
                    log.Error("Exception occured while calling method " + methodName, e);
                }
                exitThroughException = true;
                throw;
            }
            finally
            {
                if (!exitThroughException && logExit)
                {
                    TimeSpan executionTime = DateTime.Now - startTime;
                    if (returnValue == null)
                    {
                        log.LogMethodExit(methodName, "", arguments.ToString(), executionTime);
                    }
                    else
                    {
                        log.LogMethodExit(methodName, returnValue.ToString(), arguments.ToString(), executionTime);
                    }
                }
            }
        }