From 61ebeab255feee4c8d545f17df0c25728ad1cf3d Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sun, 20 Jun 2021 14:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=AB=E6=8F=8F=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Scanner/DomainAddress.cs | 3 +-- FastGithub.Scanner/GithubContext.cs | 8 +++++-- FastGithub.Scanner/GithubScanService.cs | 16 +++++++++++-- .../ScanMiddlewares/StatisticsMiddleware.cs | 23 ++----------------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/FastGithub.Scanner/DomainAddress.cs b/FastGithub.Scanner/DomainAddress.cs index 5f7989a..18543a0 100644 --- a/FastGithub.Scanner/DomainAddress.cs +++ b/FastGithub.Scanner/DomainAddress.cs @@ -46,8 +46,7 @@ namespace FastGithub.Scanner public override string ToString() { - return $"{this.Domain} {this.Address}"; + return $"{this.Domain}=>{this.Address}"; } - } } diff --git a/FastGithub.Scanner/GithubContext.cs b/FastGithub.Scanner/GithubContext.cs index 315003f..8c2f55b 100644 --- a/FastGithub.Scanner/GithubContext.cs +++ b/FastGithub.Scanner/GithubContext.cs @@ -52,10 +52,14 @@ namespace FastGithub.Scanner return base.Equals(other); } - public override string ToString() + /// + /// 转换为为统计信息 + /// + /// + public string ToStatisticsString() { 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}}"; } } } diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index 3202cf1..5b0be45 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -16,6 +16,7 @@ namespace FastGithub.Scanner { private readonly GithubLookupFacotry lookupFactory; private readonly GithubContextCollection scanResults; + private readonly ILoggerFactory loggerFactory; private readonly ILogger logger; private readonly InvokeDelegate fullScanDelegate; @@ -32,10 +33,12 @@ namespace FastGithub.Scanner GithubLookupFacotry lookupFactory, GithubContextCollection scanResults, IServiceProvider appService, + ILoggerFactory loggerFactory, ILogger logger) { this.lookupFactory = lookupFactory; this.scanResults = scanResults; + this.loggerFactory = loggerFactory; this.logger = logger; this.fullScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) @@ -72,9 +75,9 @@ namespace FastGithub.Scanner async Task ScanAsync(GithubContext 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; } @@ -97,6 +100,15 @@ namespace FastGithub.Scanner foreach (var context in contexts) { 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}条记录"); diff --git a/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs index 8e47c9f..1d95f5c 100644 --- a/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs +++ b/FastGithub.Scanner/ScanMiddlewares/StatisticsMiddleware.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using System; using System.Diagnostics; using System.Threading.Tasks; @@ -12,17 +11,6 @@ namespace FastGithub.Scanner.ScanMiddlewares [Service(ServiceLifetime.Singleton)] sealed class StatisticsMiddleware : IMiddleware { - private readonly ILogger logger; - - /// - /// 扫描统计中间件 - /// - /// - public StatisticsMiddleware(ILogger logger) - { - this.logger = logger; - } - /// /// 记录扫描结果 /// @@ -41,16 +29,9 @@ namespace FastGithub.Scanner.ScanMiddlewares 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()); - } - } } + + context.History.Add(context.Available, stopwatch.Elapsed); } } }