LoggingBehaviour.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.Extensions.Logging;
  3. namespace AipGateway.API.Application.Pipeline.Middlewares.Behaviours
  4. {
  5. public class LoggingBehaviour
  6. {
  7. private readonly ILogger<HttpRequest> _log;
  8. //private readonly ICurrentUserService _currentUserService;
  9. private readonly RequestDelegate _next;
  10. public LoggingBehaviour(ILogger<HttpRequest> logger, RequestDelegate next)
  11. {
  12. _log = logger;
  13. //_currentUserService = currentUserService;
  14. _next = next;
  15. _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: LoggingBehaviour");
  16. }
  17. public async Task InvokeAsync(HttpContext context)
  18. {
  19. //_log.LogError("LoggingBehaviour Invoke Before:");
  20. //var requestName = context.Request.Path;
  21. //var requestedPath = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path} {context.Request.QueryString}".Trim();
  22. //var userName = "Swagger";
  23. //if (!string.IsNullOrEmpty(_currentUserService.Email))
  24. //{
  25. // userName = _currentUserService.Email;
  26. //}
  27. await _next(context);
  28. await Task.FromResult(Task.CompletedTask);
  29. _log.LogError("********************************************************************************* LoggingBehaviour: BEFORE");
  30. //_log.LogError("LoggingBehaviour Invoke After:");
  31. //_log.LogError("API Request Invoke After: {Name} {@RequestedPath} {@UserName}", requestName, requestedPath, userName);
  32. //Thread.Sleep(5000);
  33. _ = ProcessAdditionalWorkAsync();
  34. return;
  35. }
  36. private async Task ProcessAdditionalWorkAsync()
  37. {
  38. try
  39. {
  40. await Task.Delay(TimeSpan.FromSeconds(10));
  41. _log.LogError("********************************************************************************* LoggingBehaviour: AFTER");
  42. }
  43. catch (Exception ex)
  44. {
  45. _log.LogError("********************************************************************************* LoggingBehaviour: AFTER {0}", ex.Message);
  46. }
  47. }
  48. }
  49. }