代码拉取完成,页面将自动刷新
using Newtonsoft.Json;
using System.Net;
using Scheduler.Model;
using Scheduler.Common.Helper;
namespace Scheduler.WebAPI
{
/// <summary>
/// 异常处理中间件
/// </summary>
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostEnvironment _environment;
private readonly ILogger<ExceptionMiddleware> _logger;
public ExceptionMiddleware(RequestDelegate next, IHostEnvironment environment, ILogger<ExceptionMiddleware> logger)
{
_next = next;
_environment = environment;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
string errorMsg = $"错误消息:{exception.Message}{Environment.NewLine}错误追踪:{exception.StackTrace}";
_logger.LogError(errorMsg);
string message = _environment.IsDevelopment() ? exception.Message : "抱歉,服务端出错了!";
var errorResult = MessageModel<object>.ErrorResult(message);
return context.Response.WriteAsync(JsonConvert.SerializeObject(errorResult));
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。