Beispiel #1
0
        /// <summary>
        /// This method contains the logic that determines what should be written
        /// to the log, if anything. It analyzes the method that is being (or was
        /// just) called, parameters that are being (or were) passed, etc., and
        /// creates log text based on those values.
        /// </summary>
        private void CreateLogEntry(MethodExecutionArgs args, String action)
        {
            // We'll use depth to determine if this is firing for a method that
            // should be logged.
            Int32 depth = 0;

            // What is the name of the method that we're entering/exiting?
            String methodName = args.Method.Name;

            // We'll use the name of the method to determine the depth of the log
            // entry to be written.
            if (methodName.Contains("Write"))
            {
                depth = 1;
            }
            else if (methodName.Contains("Generate"))
            {
                depth = 2;
            }

            // Provided we have a depth greater than zero (i.e., we recognize the
            // current method as one for which we want to log entry/exit), we'll go
            // ahead and log the action and name of the method we're in.
            if (depth > 0)
            {
                String logMessage = action + " " + methodName;
                LoggingSupport.WriteToLog(logMessage, depth);
            }
        }
        private String GenerateLine01()
        {
            LoggingSupport.WriteToLog("Entering Method GenerateLine1", 2);
            const String lineCacheKey = "TESTAPP_GenerateLine1_KEY";
            String       whatToWrite;

            try
            {
                var    aspNetCache      = HttpContext.Current.Cache;
                Object targetLineObject = aspNetCache[lineCacheKey];
                if (targetLineObject == null)
                {
                    targetLineObject = "It is by caffeine alone that I set my mind in motion.\n";
                    aspNetCache.Add(lineCacheKey, targetLineObject, null, Cache.NoAbsoluteExpiration,
                                    TimeSpan.FromMinutes(15), CacheItemPriority.Default, null);
                }
                whatToWrite = targetLineObject.ToString();
            }
            catch (Exception ex)
            {
                var newAppException = new Exception("Unexpected problem generating line 1", ex);
                LoggingSupport.WriteToLog(newAppException.ToString(), 3);
                LoggingSupport.WriteToLog("Exiting Method GenerateLine1 due to exception", 2);
                throw newAppException;
            }

            LoggingSupport.WriteToLog("Exiting Method GenerateLine1", 2);
            return(whatToWrite);
        }
        /// <summary>
        /// The OnExit method fires on the join point that occurs just after
        /// a method is exited and its execution is complete.
        /// </summary>
        public override void OnExit(MethodExecutionArgs args)
        {
            var stopWatch = args.MethodExecutionTag as Stopwatch;

            stopWatch.Stop();
            LoggingSupport.WriteToLog(String.Format("Stopwatch stopped for '{0}' method. Elapsed time: {1} ms.",
                                                    args.Method.Name, stopWatch.ElapsedMilliseconds));
        }
        /// <summary>
        /// The OnEntry method fires on the join point that occurs just before
        /// a method is entered and its first lines of code are executed.
        /// </summary>
        public override void OnEntry(MethodExecutionArgs args)
        {
            // Start the stopwatch!
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            args.MethodExecutionTag = stopwatch;
            LoggingSupport.WriteToLog(String.Format("Stopwatch started for '{0}' method.", args.Method.Name));
        }
        private String GenerateLine1()
        {
            LoggingSupport.WriteToLog("Entering Method GenerateLine1", 2);
            String whatToWrite;

            try
            {
                whatToWrite = "It is by caffeine alone that I set my mind in motion.\n";
            }
            catch (Exception ex)
            {
                var newAppException = new Exception("Unexpected problem generating line 1", ex);
                LoggingSupport.WriteToLog(newAppException.ToString(), 3);
                throw newAppException;
            }

            LoggingSupport.WriteToLog("Exiting Method GenerateLine1", 2);
            return(whatToWrite);
        }
        /// <summary>
        /// Whenever a MethodInterceptionAspect is applied to a method,
        /// execution of that method is bypassed and control is passed
        /// to the <c>OnInvoke</c> method of the aspect. The aspect then
        /// has the option of invoking the method to which the aspect is
        /// applied, carrying out it's own actions, or a combination of
        /// the two.
        /// </summary>
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            try
            {
                args.Proceed();
            }
            catch (Exception ex)
            {
                // Something went wrong calling the targeted method. We
                // need to log the problem.
                LoggingSupport.WriteToLog(String.Format("Exception encountered " +
                                                        "executing the '{0}'.\n - Exception Message: '{1}'",
                                                        args.Method.Name, ex.Message));

                // Chances are the method we're intercepting passes back a
                // String, so assign something funny for return.
                args.ReturnValue = "When Chuck Norris calls a web service, the " +
                                   "network connection is optional. The service still replies.";
            }
        }
        /// <summary>
        /// This method is invoked whenever an associated property's "get"
        /// accessor is called. We have the opportunity to carry out additional
        /// action, as well as call the actual "get"
        /// </summary>
        public override void OnGetValue(LocationInterceptionArgs args)
        {
            // Attempt to fetch the desired property from the ASP.NET Cache.
            String cacheKey            = String.Format(CACHE_KEY_TEMPLATE, args.LocationName);
            Object remotePropertyValue = HttpContext.Current.Cache[cacheKey];

            // Did we get anything back?
            if (remotePropertyValue == null)
            {
                // Pause here by locking to ensure that only one caller actually makes
                // the call to retrieve the property value.
                lock (_remotePropertyLockObject)
                {
                    remotePropertyValue = HttpContext.Current.Cache[cacheKey];
                    if (remotePropertyValue == null)
                    {
                        // The property value isn't available in the Cache, so we need to
                        // fetch it, store it, and pass it back.
                        args.ProceedGetValue();
                        LoggingSupport.WriteToLog(args.LocationName + " property value fetched from source.");
                        HttpContext.Current.Cache.Insert(cacheKey, args.Value);
                    }
                    else
                    {
                        // Property wasn't initially in cache, but another thread (in ahead of the
                        // current one) populated it.
                        LoggingSupport.WriteToLog(args.LocationName + " property value fetched from ASP.NET Cache.");
                        args.Value = remotePropertyValue;
                    }
                }
            }
            else
            {
                // Simply assign the property value from the Cache.
                LoggingSupport.WriteToLog(args.LocationName + " property value fetched from ASP.NET Cache.");
                args.Value = remotePropertyValue;
            }
        }