using Furion; using Furion.DatabaseAccessor; using Furion.DependencyInjection; using Furion.DistributedIDGenerator; using Furion.FriendlyException; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FlexJobApi.Core { public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton { private readonly IServiceScope serviceScope; private readonly IRepository rep; public LogExceptionHandler(IServiceScopeFactory scopeFactory) { serviceScope = scopeFactory.CreateScope(); rep = serviceScope.ServiceProvider.GetRequiredService>(); } public async Task OnExceptionAsync(ExceptionContext context) { Console.WriteLine(App.User); await rep.InsertNowAsync(new ExceptionLog { Id = IDGen.NextID(), TraceId = App.GetTraceId(), Type = context.Exception.GetType().Name, Message = context.Exception.Message, StackTrace = context.Exception.StackTrace, CreatedTime = DateTime.Now, }); if (context.Exception.InnerException != null) { await rep.InsertNowAsync(new ExceptionLog { Id = IDGen.NextID(), TraceId = App.GetTraceId(), Type = context.Exception.InnerException.GetType().Name, Message = context.Exception.InnerException.Message, StackTrace = context.Exception.InnerException.StackTrace, CreatedTime = DateTime.Now, }); } } } }