public override void OnException(ExceptionContext filterContext)
        {
            Exception loggingException = null;

            try
            {
                var    innerText = new StringBuilder();
                string body      = null;
                try
                {
                    var e = filterContext.Exception?.InnerException;
                    while (e != null)
                    {
                        innerText.Append(e.Message + Environment.NewLine);
                        e = e.InnerException;
                    }
                }
                catch { } // Ignore errors

                try
                {
                    using (Stream receiveStream = filterContext.HttpContext.Request.InputStream)
                    {
                        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                        {
                            receiveStream.Seek(0, SeekOrigin.Begin);
                            body = readStream.ReadToEnd();
                        }
                    }
                }
                catch { } // Ignore errors

                var exception = new UnhandledControllerException()
                {
                    Message = filterContext.Exception.Message,
                    InnerExceptionMessages = innerText.ToString(),
                    StackTrace             = filterContext.Exception.StackTrace,
                    Url      = filterContext.HttpContext.Request.Url.ToString(),
                    Body     = body,
                    UserId   = filterContext.HttpContext.User.Identity.GetUserId(),
                    DateTime = DateTimeOffset.UtcNow
                };

                Repository.InsertUnhandledControllerException(exception);
            }
            catch (Exception e)
            {
                loggingException = e;
            }

            base.OnException(filterContext);

            if (filterContext.Result is ViewResult viewResult)
            {
                viewResult.ViewBag.LoggingException = loggingException;
            }
        }
        public async override Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            var    innerText = new StringBuilder();
            string body      = null;

            try
            {
                var e = context.Exception?.InnerException;
                while (e != null)
                {
                    innerText.Append(e.Message + Environment.NewLine);
                    e = e.InnerException;
                }
            }
            catch { } // Ignore errors

            try
            {
                HttpContext.Current.Request.InputStream.Position = 0;
                using (var reader = new StreamReader(HttpContext.Current.Request.InputStream, System.Text.Encoding.UTF8, true, 4096, true))
                {
                    body = await reader.ReadToEndAsync();
                }
                HttpContext.Current.Request.InputStream.Position = 0;
            }
            catch { } // Ignore errors

            var exception = new UnhandledControllerException()
            {
                Message = context.Exception.Message,
                InnerExceptionMessages = innerText.ToString(),
                StackTrace             = context.Exception.StackTrace,
                Url      = context.Request.RequestUri.ToString(),
                Body     = body,
                UserId   = context.RequestContext.Principal.Identity.GetUserId(),
                DateTime = DateTimeOffset.UtcNow
            };

            Repository.InsertUnhandledControllerException(exception);

            await base.HandleAsync(context, cancellationToken);
        }
示例#3
0
        // Unhandled exceptions

        public void InsertUnhandledControllerException(UnhandledControllerException exception)
        {
            context.UnhandledControllerExceptions.Add(exception);
            context.SaveChanges();
        }