完善扫描日志

This commit is contained in:
xljiulang 2021-06-20 14:37:01 +08:00
parent aa4755f45f
commit 61ebeab255
4 changed files with 23 additions and 27 deletions

View File

@ -46,8 +46,7 @@ namespace FastGithub.Scanner
public override string ToString() public override string ToString()
{ {
return $"{this.Domain} {this.Address}"; return $"{this.Domain}=>{this.Address}";
} }
} }
} }

View File

@ -52,10 +52,14 @@ namespace FastGithub.Scanner
return base.Equals(other); return base.Equals(other);
} }
public override string ToString() /// <summary>
/// 转换为为统计信息
/// </summary>
/// <returns></returns>
public string ToStatisticsString()
{ {
var rate = Math.Round(this.History.AvailableRate * 100, 2); var rate = Math.Round(this.History.AvailableRate * 100, 2);
return $"{this.Domain} {{Address={this.Address}, AvailableRate={rate}%, AvgElapsed={this.History.AvgElapsed.TotalSeconds}s}}"; return $"{{Address={this.Address}, AvailableRate={rate}%, AvgElapsed={this.History.AvgElapsed.TotalSeconds}s}}";
} }
} }
} }

View File

@ -16,6 +16,7 @@ namespace FastGithub.Scanner
{ {
private readonly GithubLookupFacotry lookupFactory; private readonly GithubLookupFacotry lookupFactory;
private readonly GithubContextCollection scanResults; private readonly GithubContextCollection scanResults;
private readonly ILoggerFactory loggerFactory;
private readonly ILogger<GithubScanService> logger; private readonly ILogger<GithubScanService> logger;
private readonly InvokeDelegate<GithubContext> fullScanDelegate; private readonly InvokeDelegate<GithubContext> fullScanDelegate;
@ -32,10 +33,12 @@ namespace FastGithub.Scanner
GithubLookupFacotry lookupFactory, GithubLookupFacotry lookupFactory,
GithubContextCollection scanResults, GithubContextCollection scanResults,
IServiceProvider appService, IServiceProvider appService,
ILoggerFactory loggerFactory,
ILogger<GithubScanService> logger) ILogger<GithubScanService> logger)
{ {
this.lookupFactory = lookupFactory; this.lookupFactory = lookupFactory;
this.scanResults = scanResults; this.scanResults = scanResults;
this.loggerFactory = loggerFactory;
this.logger = logger; this.logger = logger;
this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask) this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
@ -72,9 +75,9 @@ namespace FastGithub.Scanner
async Task<bool> ScanAsync(GithubContext context) async Task<bool> ScanAsync(GithubContext context)
{ {
await this.fullScanDelegate(context); await this.fullScanDelegate(context);
if (context.Available == true) if (context.Available && this.scanResults.Add(context))
{ {
this.scanResults.Add(context); this.logger.LogInformation($"扫描到{context}");
} }
return context.Available; return context.Available;
} }
@ -97,6 +100,15 @@ namespace FastGithub.Scanner
foreach (var context in contexts) foreach (var context in contexts)
{ {
await this.resultScanDelegate(context); await this.resultScanDelegate(context);
var domainLogger = this.loggerFactory.CreateLogger(context.Domain);
if (context.Available == true)
{
domainLogger.LogInformation(context.ToStatisticsString());
}
else
{
domainLogger.LogWarning(context.ToStatisticsString());
}
} }
this.logger.LogInformation($"结果扫描结束,共扫描{results.Length}条记录"); this.logger.LogInformation($"结果扫描结束,共扫描{results.Length}条记录");

View File

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,17 +11,6 @@ namespace FastGithub.Scanner.ScanMiddlewares
[Service(ServiceLifetime.Singleton)] [Service(ServiceLifetime.Singleton)]
sealed class StatisticsMiddleware : IMiddleware<GithubContext> sealed class StatisticsMiddleware : IMiddleware<GithubContext>
{ {
private readonly ILogger<StatisticsMiddleware> logger;
/// <summary>
/// 扫描统计中间件
/// </summary>
/// <param name="logger"></param>
public StatisticsMiddleware(ILogger<StatisticsMiddleware> logger)
{
this.logger = logger;
}
/// <summary> /// <summary>
/// 记录扫描结果 /// 记录扫描结果
/// </summary> /// </summary>
@ -41,16 +29,9 @@ namespace FastGithub.Scanner.ScanMiddlewares
finally finally
{ {
stopwatch.Stop(); stopwatch.Stop();
if (context.CancellationToken.IsCancellationRequested == false)
{
context.History.Add(context.Available, stopwatch.Elapsed);
if (context.History.AvailableRate > 0d)
{
this.logger.LogInformation(context.ToString());
}
}
} }
context.History.Add(context.Available, stopwatch.Elapsed);
} }
} }
} }