Example #1
0
        /// <summary>
        ///     Fired when an exception occurs.
        /// </summary>
        /// <param name="filterContext">The exception context.</param>
        public override void OnException(ExceptionContext filterContext)
        {
            // Get the exception
            var ex = filterContext.Exception;

            // Log it
            try
            {
                var mailer = new SmtpMailer();
                Task.Run(async () => await mailer.Send(_from, _to, string.Format(_subjectFormat, ex.GetType()), ex.ToString())).Wait();
            }
            catch
            {
                // Exception handler CANNOT raise an exception!
            }

            // Set exception handled
            filterContext.ExceptionHandled = true;
        }
        // THe subject format
        /// <summary>
        ///     The constructor.
        /// </summary>
        /// <param name="smtpMailer">The SMTP mailer to use.</param>
        /// <param name="from">The from email address.</param>
        /// <param name="to">The to email address.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when smtpMailer is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown when from or to is null, empty, or white space.</exception>
        public EmailErrorAttribute(SmtpMailer smtpMailer, string from, string to)
        {
            // Sanitize
            if (smtpMailer == null)
            {
                throw new ArgumentNullException("smtpMailer");
            }
            if (string.IsNullOrWhiteSpace(from))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "from");
            }
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException("cannot be null, empty, or white space", "to");
            }

            // Set fields
            _smtpMailer = smtpMailer;
            _from = from;
            _to = to;
        }