From aadb99957eae52d1b066305818bef1c91b949a01 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 24 Aug 2019 01:58:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=81=A5=E5=BA=B7=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E7=A7=BB=E5=8A=A8=E5=88=B0=E9=80=9A=E7=94=A8=E7=B1=BB?= =?UTF-8?q?=E5=BA=93=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bootstrap.Security.Mvc 升级到 2.2.12 增加健康检查 --- Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +- Bootstrap.Admin/HealthChecks/FileHealCheck.cs | 46 ------------------- Bootstrap.Admin/HealthChecks/GCHealthCheck.cs | 34 -------------- .../HealthChecksAppBuilderExtensions.cs | 45 ------------------ .../HealthChecksBuilderExtensions.cs | 11 ++--- .../HealthChecks/MemoryHealthCheck.cs | 34 -------------- Bootstrap.Admin/Startup.cs | 2 +- Bootstrap.Client/Bootstrap.Client.csproj | 2 +- 8 files changed, 8 insertions(+), 168 deletions(-) delete mode 100644 Bootstrap.Admin/HealthChecks/FileHealCheck.cs delete mode 100644 Bootstrap.Admin/HealthChecks/GCHealthCheck.cs delete mode 100644 Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs delete mode 100644 Bootstrap.Admin/HealthChecks/MemoryHealthCheck.cs diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj index 5a756aad..298bd8e8 100644 --- a/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -10,7 +10,7 @@ - + diff --git a/Bootstrap.Admin/HealthChecks/FileHealCheck.cs b/Bootstrap.Admin/HealthChecks/FileHealCheck.cs deleted file mode 100644 index 591409f9..00000000 --- a/Bootstrap.Admin/HealthChecks/FileHealCheck.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Diagnostics.HealthChecks; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace Bootstrap.Admin.HealthChecks -{ - /// - /// 文件健康检查类 - /// - public class FileHealCheck : IHealthCheck - { - private readonly IHostingEnvironment _env; - /// - /// 构造函数 - /// - /// - public FileHealCheck(IHostingEnvironment env) - { - _env = env; - } - - /// - /// 异步检查方法 - /// - /// - /// - /// - public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) - { - var file = _env.IsDevelopment() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Longbow.lic") : Path.Combine(_env.ContentRootPath, "Longbow.lic"); - var data = new Dictionary - { - { "ApplicationName", _env.ApplicationName }, - { "EnvironmentName", _env.EnvironmentName }, - { "ContentRootPath", _env.ContentRootPath }, - { "WebRootPath", _env.WebRootPath }, - { "CheckFile", file } - }; - return Task.FromResult(File.Exists(file) ? HealthCheckResult.Healthy("Ok", data) : HealthCheckResult.Unhealthy($"Missing file {file}", null, data)); - } - } -} diff --git a/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs b/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs deleted file mode 100644 index 9692f45c..00000000 --- a/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace Bootstrap.Admin.HealthChecks -{ - /// - /// 内存状态检查其 - /// - public class GCHealthCheck : IHealthCheck - { - /// - /// 异步检查方法 - /// - /// - /// - /// - public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) - { - // Include GC information in the reported diagnostics. - var allocated = GC.GetTotalMemory(forceFullCollection: false); - var data = new Dictionary() - { - { "AllocatedMBytes", allocated / 1024 / 1024 }, - { "Gen0Collections", GC.CollectionCount(0) }, - { "Gen1Collections", GC.CollectionCount(1) }, - { "Gen2Collections", GC.CollectionCount(2) }, - }; - return Task.FromResult(HealthCheckResult.Healthy("OK", data)); - } - } -} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs deleted file mode 100644 index 3718ebc1..00000000 --- a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Diagnostics.HealthChecks; -using Newtonsoft.Json; -using System.Threading.Tasks; - -namespace Microsoft.AspNetCore.Builder -{ - /// - /// BootstrapAdmin 健康检查扩展类 - /// - public static class HealthChecksAppBuilderExtensions - { - /// - /// 启用健康检查 - /// - /// - /// - /// - public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app, PathString path = default) - { - if (path == default) path = "/Healths"; - app.UseHealthChecks(path, new HealthCheckOptions() - { - ResponseWriter = (context, report) => - { - context.Response.ContentType = "application/json"; - return context.Response.WriteAsync(JsonConvert.SerializeObject(new { report.Entries.Keys, Report = report })); - }, - ResultStatusCodes = - { - [HealthStatus.Healthy] = StatusCodes.Status200OK, - [HealthStatus.Degraded] = StatusCodes.Status200OK, - [HealthStatus.Unhealthy] = StatusCodes.Status200OK - } - }); - app.UseWhen(context => context.Request.Path == "/healths-ui", builder => builder.Run(request => - { - request.Response.Redirect("/html/Healths-UI.html"); - return Task.CompletedTask; - })); - return app; - } - } -} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs index 2bdd2b93..ebcb5d58 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -10,16 +10,15 @@ namespace Microsoft.Extensions.DependencyInjection /// /// 添加 BootstrapAdmin 健康检查 /// - /// + /// /// - public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) + public static IServiceCollection AddAdminHealthChecks(this IServiceCollection services) { + var builder = services.AddHealthChecks(); builder.AddCheck("db"); - builder.AddCheck("file"); - builder.AddCheck("gc"); - builder.AddCheck("mem"); + builder.AddBootstrapAdminHealthChecks(); builder.AddCheck("Gitee"); - return builder; + return services; } } } diff --git a/Bootstrap.Admin/HealthChecks/MemoryHealthCheck.cs b/Bootstrap.Admin/HealthChecks/MemoryHealthCheck.cs deleted file mode 100644 index 6aaba559..00000000 --- a/Bootstrap.Admin/HealthChecks/MemoryHealthCheck.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using System.Collections.Generic; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; - -namespace Bootstrap.Admin.HealthChecks -{ - /// - /// 内存检查器 - /// - public class MemoryHealthCheck : IHealthCheck - { - /// - /// 异步检查方法 - /// - /// - /// - /// - public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) - { - var process = Process.GetCurrentProcess(); - var data = new Dictionary() - { - { "Id", process.Id }, - { "WorkingSet", process.WorkingSet64 }, - { "PrivateMemory", process.PrivateMemorySize64 }, - { "VirtualMemory", process.VirtualMemorySize64 }, - }; - - return Task.FromResult(HealthCheckResult.Healthy("Ok", data)); - } - } -} diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index c5a770b0..7b0fe510 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -65,7 +65,7 @@ namespace Bootstrap.Admin services.AddButtonAuthorization(); services.AddDemoTask(); services.AddHttpClient(); - services.AddHealthChecks().AddBootstrapAdminHealthChecks(); + services.AddAdminHealthChecks(); services.AddMvc(options => { options.Filters.Add(); diff --git a/Bootstrap.Client/Bootstrap.Client.csproj b/Bootstrap.Client/Bootstrap.Client.csproj index c2d9a294..8df541b8 100644 --- a/Bootstrap.Client/Bootstrap.Client.csproj +++ b/Bootstrap.Client/Bootstrap.Client.csproj @@ -6,7 +6,7 @@ - +