Browse Source

完善扫描日志

xljiulang 4 years ago
parent
commit
61ebeab255

+ 1 - 2
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}";
         }
-
     }
 }

+ 6 - 2
FastGithub.Scanner/GithubContext.cs

@@ -52,10 +52,14 @@ namespace FastGithub.Scanner
             return base.Equals(other);
         }
 
-        public override string ToString()
+        /// <summary>
+        /// 转换为为统计信息
+        /// </summary>
+        /// <returns></returns>
+        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}}";
         }
     }
 }

+ 14 - 2
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<GithubScanService> logger;
 
         private readonly InvokeDelegate<GithubContext> fullScanDelegate;
@@ -32,10 +33,12 @@ namespace FastGithub.Scanner
             GithubLookupFacotry lookupFactory,
             GithubContextCollection scanResults,
             IServiceProvider appService,
+            ILoggerFactory loggerFactory,
             ILogger<GithubScanService> logger)
         {
             this.lookupFactory = lookupFactory;
             this.scanResults = scanResults;
+            this.loggerFactory = loggerFactory;
             this.logger = logger;
 
             this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
@@ -72,9 +75,9 @@ namespace FastGithub.Scanner
             async Task<bool> 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}条记录");

+ 2 - 21
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<GithubContext>
     {
-        private readonly ILogger<StatisticsMiddleware> logger;
-
-        /// <summary>
-        /// 扫描统计中间件
-        /// </summary>
-        /// <param name="logger"></param>
-        public StatisticsMiddleware(ILogger<StatisticsMiddleware> logger)
-        {
-            this.logger = logger;
-        }
-
         /// <summary>
         /// 记录扫描结果
         /// </summary>
@@ -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);
         }
     }
 }