Esempio n. 1
0
        /// <summary>
        ///  Called when an event has been trapped by Bad Behavior. Can be overridden
        ///  to provide custom behaviour when an event has been logged.
        /// </summary>
        /// <param name="args">
        ///  A <see cref="BadBehaviorEventArgs"/> object containing information about
        ///  the bad request.
        /// </param>

        protected virtual void OnBadBehavior(BadBehaviorEventArgs args)
        {
            if (this.BadBehavior != null)
                this.BadBehavior(this, args);
        }
Esempio n. 2
0
        /// <summary>
        ///  Called when an error or suspicious condition has been raised.
        /// </summary>
        /// <param name="package">
        ///  The <see cref="Package"/> instance containing details of the request.
        /// </param>
        /// <param name="error">
        ///  The error condition detailing the problem.
        /// </param>
        /// <param name="strict">
        ///  true if this is a strict condition (i.e. should only be trapped when
        ///  running in strict mode); otherwise false.
        /// </param>

        protected virtual void Raise(Package package, Error error, bool strict)
        {
            bool thrown = this.Settings.Strict || !strict;
            var ex = new BadBehaviorException(package, error);
            try {
                Logger.Log(new LogEntry(ex, thrown));
                var args = new BadBehaviorEventArgs(package, error);
                OnBadBehavior(args);
            }
            catch (Exception loggingException) {
                /*
                 * An exception when logging or running the event handler needs to
                 * be trapped here, for two reasons. First, logging must not bring
                 * anything to a halt under any circumstances. Second, if we throw
                 * an "uninteresting" (ie non-BB) exception here, it will be
                 * swallowed higher up the call stack in ValidateRequest above,
                 * and the request will be given the all clear when it should have
                 * been rejected.
                 * 
                 * So, Pokémon it and log it to System.Diagnostics.Trace.
                 */
                if (loggingException is BadBehaviorException || this.Settings.Debug)
                    throw;
                else
                    Trace.TraceWarning(
                        "An error was encountered when attempting to validate this request. "
                        + "Exception details: " + loggingException.ToString()
                    );
            }
            if (thrown) {
                throw ex;
            }
        }