平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
959 B

  1. //
  2. using MoYu.DependencyInjection;
  3. using MoYu.FriendlyException;
  4. using Microsoft.Extensions.Logging;
  5. namespace SafeCampus.Web.Core;
  6. /// <summary>
  7. /// 全局异常处理提供器
  8. /// </summary>
  9. public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton
  10. {
  11. private readonly ILogger<LogExceptionHandler> _logger;
  12. public LogExceptionHandler(ILogger<LogExceptionHandler> logger)
  13. {
  14. _logger = logger;
  15. }
  16. public async Task OnExceptionAsync(ExceptionContext context)
  17. {
  18. var exception = context.Exception;//获取异常
  19. //如果异常类型不是友好异常
  20. if (exception.GetType() != typeof(AppFriendlyException))
  21. {
  22. _logger.LogError(exception, exception.Message);
  23. //重新定义异常
  24. context.Exception = new AppFriendlyException("系统异常,请联系管理员", ErrorCodeEnum.A0000);
  25. }
  26. await Task.CompletedTask;
  27. }
  28. }