UnhandledExceptionBehaviour.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.Extensions.Logging;
  3. namespace AipGateway.API.Application.Pipeline.Middlewares.Behaviours
  4. {
  5. public class UnhandledExceptionBehaviour
  6. {
  7. private readonly ILogger<HttpRequest> _log;
  8. private readonly RequestDelegate _next;
  9. public UnhandledExceptionBehaviour(ILogger<HttpRequest> logger, RequestDelegate next)
  10. {
  11. _log = logger;
  12. _next = next;
  13. _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: UnhandledExceptionBehaviour");
  14. }
  15. public async Task InvokeAsync(HttpContext context)
  16. {
  17. try
  18. {
  19. //_log.LogError("UnhandledExceptionBehaviour Invoke Before:");
  20. await _next.Invoke(context);
  21. //_log.LogError("UnhandledExceptionBehaviour Invoke After:");
  22. _log.LogError("UnhandledExceptionBehaviour.........................................................................");
  23. }
  24. catch (Exception ex)
  25. {
  26. var requestName = context.Request.Path;
  27. _log.LogError(ex, "API Request: Unhandled Exception for Request {Name}", requestName);
  28. throw;
  29. }
  30. }
  31. }
  32. }