From c0bf528ba67619d8d03406490f588c898bca644b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Thu, 20 Apr 2023 19:45:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=20GlobalExceptionFilter.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Filters/GlobalExceptionFilter.cs | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 ZR.Admin.WebApi/Filters/GlobalExceptionFilter.cs diff --git a/ZR.Admin.WebApi/Filters/GlobalExceptionFilter.cs b/ZR.Admin.WebApi/Filters/GlobalExceptionFilter.cs deleted file mode 100644 index b0f205d..0000000 --- a/ZR.Admin.WebApi/Filters/GlobalExceptionFilter.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Diagnostics; -using Infrastructure; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; - -namespace ZRAdmin.Filters -{ - /// - /// 全局异常捕获 - /// 暂时没用了,改为中间件 2021-2-26 - /// - public class GlobalExceptionFilter : IExceptionFilter - { - readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - - private IWebHostEnvironment _hostingEnvironment; - - public GlobalExceptionFilter(IWebHostEnvironment environment) - { - _hostingEnvironment = environment; - } - - public void OnException(ExceptionContext context) - { - Exception ex = context.Exception; - - int code = (int)ResultCode.GLOBAL_ERROR; - string msg; - //自定义异常 - if (ex is CustomException customException) - { - code = customException.Code; - msg = customException.Message; - - logger.Info($"自定义异常code={code},msg={msg}"); - } - else - //参数异常 - if (ex is ArgumentException) - { - code = (int)ResultCode.PARAM_ERROR; - msg = ex.Message; - } - else - { - msg = "服务器好像出了点问题......"; - Console.WriteLine(ex.StackTrace); - // 获取异常堆栈 - var traceFrame = new StackTrace(ex, true).GetFrame(0); - // 获取出错的文件名 - var exceptionFileName = traceFrame.GetFileName(); - // 获取出错的行号 - var exceptionFileLineNumber = traceFrame.GetFileLineNumber(); - var errorMsg = $"{ex.Message}##{traceFrame}##{exceptionFileName}##行号:{exceptionFileLineNumber}"; - - logger.Error(ex, $"系统出错了error={ex.Message}#{errorMsg}"); - //context.Response.StatusCode = 500; - } - - context.Result = new JsonResult(new { code, msg }); - context.ExceptionHandled = true; - } - - } -} \ No newline at end of file