FastGithub/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs
2021-06-17 17:50:44 +08:00

41 lines
1.1 KiB
C#

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<GithubContext>
{
private readonly ILogger<StatisticsMiddleware> logger;
public StatisticsMiddleware(ILogger<StatisticsMiddleware> logger)
{
this.logger = logger;
}
public async Task InvokeAsync(GithubContext context, Func<Task> next)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
try
{
await next();
}
finally
{
stopwatch.Stop();
context.History.Add(context.Available, stopwatch.Elapsed);
if (context.History.AvailableRate > 0d)
{
this.logger.LogInformation(context.ToString());
}
}
}
}
}