using System; using Microsoft.AspNetCore.Mvc.Filters; using WebAPI.Services.Interfaces; namespace WebAPI { public class HandleExceptionFilter : IExceptionFilter { private readonly ILoggingService _loggingService; public HandleExceptionFilter(ILoggingService loggingService) { _loggingService = loggingService; } public void OnException(ExceptionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } Exception exception = context.Exception; // Log exception string requestUrl = $"{context.HttpContext.Request.Scheme}://{context.HttpContext.Request.Host}{context.HttpContext.Request.Path}{context.HttpContext.Request.QueryString}"; int errorTrackNo = _loggingService.Error(exception, requestUrl); // Optionally, you can set the result here // context.Result = new ObjectResult($"Error Track No:{errorTrackNo}") // { // StatusCode = 500 // }; } } }