我想在asp.net核心Web API应用程序上从AppMetrics库应用运行状况检查。我尝试了多种方法,但没有成功。
在Program.cs
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseHealth()
.UseStartup<Startup>()
.Build();
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<HealthCheck, SampleHealthCheck>();
var metrics = new HealthBuilder()
.HealthChecks.AddCheck(new SampleHealthCheck())
.HealthChecks.AddCheck("DatabaseConnected",
() => new ValueTask<HealthCheckResult>(HealthCheckResult.Healthy("Database Connection OK")))
.HealthChecks.RegisterFromAssembly(services)
.Build();
services.AddHealth(metrics);
services.AddHealthEndpoints();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHealthAllEndpoints();
app.UseMvc();
}
我仍然收到的错误如下:
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at at System.Reflection.RuntimeModule.GetDefinedTypes()
at at System.Reflection.RuntimeAssembly.get_DefinedTypes()
at App.Metrics.Health.Extensions.DependencyInjection.Internal.DefaultHealthCheckTypeProvider.<>c.<get_HealthCheckTypes>b__5_0(Assembly a) in C:\projects\microsoftextensions\src\App.Metrics.Health.Extensions.DependencyInjection\Internal\DefaultHealthCheckTypeProvider.cs:25
at at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at App.Metrics.Health.Extensions.DependencyInjection.Internal.HealthChecksAsServices.AddHealthChecksAsServices(IServiceCollection services, IEnumerable`1 types) in C:\projects\microsoftextensions\src\App.Metrics.Health.Extensions.DependencyInjection\Internal\HealthChecksAsServices.cs:27
at App.Metrics.Health.Extensions.DependencyInjection.Internal.HealthChecksAsServices.AddHealthChecksAsServices(IServiceCollection services, IEnumerable`1 assemblies) in C:\projects\microsoftextensions\src\App.Metrics.Health.Extensions.DependencyInjection\Internal\HealthChecksAsServices.cs:53
at Microsoft.Extensions.DependencyInjection.ServiceCollectionHealthCheckBuilderExtensions.RegisterFromAssembly(IHealthCheckBuilder healthCheckBuilder, IServiceCollection services, DependencyContext dependencyContext) in C:\projects\microsoftextensions\src\App.Metrics.Health.Extensions.DependencyInjection\ServiceCollectionHealthCheckBuilderExtensions.cs:34
at Microsoft.AspNetCore.Hosting.HealthAspNetWebHostBuilderExtensions.<>c__DisplayClass5_0.<ConfigureHealth>b__0(WebHostBuilderContext context, IServiceCollection services) in C:\projects\aspnetcorehealth\src\App.Metrics.AspNetCore.Health.Hosting\HealthAspNetWebHostBuilderExtensions.cs:113
at at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
at at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at WebApiAppMetrics.Program.BuildWebHost(String[] args) in C:\DEV\AspnetWebApiAppMetricsPlayground\AspnetWebApiAppMetricsPlayground\WebApiAppMetrics\Program.cs:24
at WebApiAppMetrics.Program.Main(String[] args) in D:\_TEST_\AspnetWebApiAppMetricsPlayground\AspnetWebApiAppMetricsPlayground\WebApiAppMetrics\Program.cs:20
答案 0 :(得分:0)
请按照以下步骤操作演示:
App.Metrics.AspNetCore.Health
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureHealthWithDefaults(
builder =>
{
builder.HealthChecks.AddCheck("DatabaseConnected",
() => new ValueTask<HealthCheckResult>(HealthCheckResult.Healthy("Database Connection OK")));
})
.UseHealth()
.UseStartup<Startup>();
}
访问https://localhost:44343/health
以检查结果。
对于App.Metrics.AspNetCore.Health
和UseHealth()
,它会自动扫描执行程序集以及依赖于App.Metrics。*的任何引用程序集,以进行运行状况检查并注册它们
对于SampleHealthCheck
,如果您继承自HealthCheck
,则无需显式注册。 AppMetrics
将为您注册。
引用HEALTH