StatisticsMiddleware.cs 956 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.Extensions.DependencyInjection;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. namespace FastGithub.Scanner.ScanMiddlewares
  6. {
  7. /// <summary>
  8. /// 扫描统计中间件
  9. /// </summary>
  10. [Service(ServiceLifetime.Singleton)]
  11. sealed class StatisticsMiddleware : IMiddleware<GithubContext>
  12. {
  13. /// <summary>
  14. /// 记录扫描结果
  15. /// </summary>
  16. /// <param name="context"></param>
  17. /// <param name="next"></param>
  18. /// <returns></returns>
  19. public async Task InvokeAsync(GithubContext context, Func<Task> next)
  20. {
  21. var stopwatch = new Stopwatch();
  22. try
  23. {
  24. stopwatch.Start();
  25. await next();
  26. }
  27. finally
  28. {
  29. stopwatch.Stop();
  30. }
  31. context.History.Add(context.Available, stopwatch.Elapsed);
  32. }
  33. }
  34. }