コード例 #1
0
        public override void OnException(ExceptionContext context)
        {
            if (context.Exception is ValidationException)
            {
                var exception = context.Exception as ValidationException;
                context.HttpContext.Response.StatusCode = 400;
                context.Result = new JsonResult(new ValidationResultModel(exception));
                return;
            }

            var exceptionId = _guid.New();

            context.HttpContext.Response.StatusCode = 500;
            var sb = new StringBuilder();

            sb.AppendLine("ErrorId: " + exceptionId);
            sb.AppendLine(context.HttpContext.Request.GetDisplayUrl());

            if (context.HttpContext.Request.Body.CanSeek)
            {
                context.HttpContext.Request.Body.Position = 0;
                using (var reader = new StreamReader(context.HttpContext.Request.Body))
                {
                    sb.AppendLine(reader.ReadToEnd());
                }
            }

            sb.AppendLine();
            sb.Append(context.Exception);

            _logger.Error(sb.ToString());

            if (_hosting.IsDevelopment())
            {
                context.Result = new ContentResult()
                {
                    Content = sb.ToString()
                };
            }
            else
            {
                context.Result = new ContentResult()
                {
                    Content = exceptionId.ToString()
                };
            }
        }