using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Diagnostics; using System.Threading.Tasks; namespace FastGithub.Scanner.ScanMiddlewares { /// /// 扫描统计中间件 /// [Service(ServiceLifetime.Singleton)] sealed class StatisticsMiddleware : IMiddleware { private readonly ILogger logger; /// /// 扫描统计中间件 /// /// public StatisticsMiddleware(ILogger logger) { this.logger = logger; } /// /// 记录扫描结果 /// /// /// /// public async Task InvokeAsync(GithubContext context, Func next) { var stopwatch = new Stopwatch(); try { stopwatch.Start(); await next(); } finally { stopwatch.Stop(); if (context.CancellationToken.IsCancellationRequested == false) { context.History.Add(context.Available, stopwatch.Elapsed); if (context.History.AvailableRate > 0d) { this.logger.LogInformation(context.ToString()); } } } } } }