106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Nirvana.Common;
|
|
using Nirvana.Common.ApiBase;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using YBDevice.Api.DBServices;
|
|
|
|
namespace YBDevice.Api
|
|
{
|
|
/// <summary>
|
|
/// 异常处理中间件
|
|
/// </summary>
|
|
//public class MyExceptionMiddleware
|
|
//{
|
|
// private readonly RequestDelegate _next;
|
|
// public MyExceptionMiddleware(RequestDelegate next)
|
|
// {
|
|
// _next = next;
|
|
// }
|
|
// public async Task Invoke(HttpContext httpContext)
|
|
// {
|
|
// try
|
|
// {
|
|
// await _next(httpContext);
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// // new ApiLoggerApplication().InsertErrorLog(ex);
|
|
// await HandleExceptionAsync(httpContext, ex);
|
|
// }
|
|
// }
|
|
|
|
// private Task HandleExceptionAsync(HttpContext httpContext, Exception ex)
|
|
// {
|
|
// //记录日志
|
|
// // new ApiLoggerApplication().InsertErrorLog(ex);
|
|
// return Task.CompletedTask;
|
|
// }
|
|
|
|
// public void OnException(ExceptionContext context)
|
|
// {
|
|
// context.ExceptionHandled = true;
|
|
// }
|
|
//}
|
|
|
|
///// <summary>
|
|
///// Extension method used to add the middleware to the HTTP request pipeline.
|
|
///// </summary>
|
|
//public static class MyExceptionMiddlewareExtensions
|
|
//{
|
|
// public static IApplicationBuilder UseMyExceptionMiddleware(this IApplicationBuilder builder)
|
|
// {
|
|
// return builder.UseMiddleware<MyExceptionMiddleware>();
|
|
// }
|
|
//}
|
|
|
|
public static class ExceptionHandlingExtensions
|
|
{
|
|
public static void UseMyExceptionHandler(this IApplicationBuilder app)
|
|
{
|
|
app.UseExceptionHandler(builder => {
|
|
|
|
builder.Run(async context =>
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
|
context.Response.ContentType = "application/json";
|
|
var ex = context.Features.Get<IExceptionHandlerFeature>();
|
|
//如果是用户取消的异常,则不必记录到日志
|
|
if(context.RequestAborted.IsCancellationRequested && (ex is TaskCanceledException || ex is OperationCanceledException))
|
|
{
|
|
Console.WriteLine("用户取消了操作");
|
|
}
|
|
else
|
|
{
|
|
if (ex != null)
|
|
{
|
|
var request = context.Request;
|
|
var action = request.Path.Value;
|
|
var query_string = request.QueryString.Value;
|
|
if (request.Method == "POST" && request.ContentLength > 0)
|
|
{
|
|
request.EnableBuffering();
|
|
request.Body.Position = 0;
|
|
using (var sr = new StreamReader(request.Body))
|
|
{
|
|
query_string = await sr.ReadToEndAsync();
|
|
}
|
|
}
|
|
//记录日志
|
|
new LoggerApp().InsertErrorLog(ex.Error, query_string, action);
|
|
}
|
|
}
|
|
var result = new ResultInfo { code = ResultState.SYSTEMERROR, message = ex?.Error?.Message ?? "系统异常" };
|
|
await context.Response.WriteAsync(result.ToJson());
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|