Skip to content

HealthCheck

Himani-ja edited this page Jan 24, 2022 · 3 revisions

Description

Health checks are a set of checks that you perform in order to tell whether an application/service is up, running & healthy or not. Health checks are very useful especially when your application depends on other things like a database or even other services.

NuGet Packages

  • AspNetCore.HealthChecks.UI
  • AspNetCore.HealthChecks.UI.Client

HealthChecks UI offers several storage providers, available as different nuget packages.

  • AspNetCore.HealthChecks.UI.InMemory.Storage
  • AspNetCore.HealthChecks.UI.SqlServer.Storage
  • AspNetCore.HealthChecks.UI.SQLite.Storage
  • AspNetCore.HealthChecks.UI.PostgreSQL.Storage
  • AspNetCore.HealthChecks.UI.MySql.Storage

HealthChecks packages include health checks for:

  • Sql Server
  • MySql
  • Oracle
  • Sqlite
  • RavenDB
  • Postgres
  • EventStore
  • RabbitMQ
  • IbmMQ
  • Elasticsearch
  • CosmosDb
  • Solr
  • Redis
  • SendGrid
  • System: Disk Storage, Private Memory, Virtual Memory, Process, Windows Service
  • Azure Service Bus: EventHub, Queue and Topics
  • Azure Storage: Blob, Queue and Table
  • Azure Key Vault
  • Azure DocumentDb
  • Azure IoT Hub
  • Amazon DynamoDb
  • Amazon S3
  • Google Cloud Firestore
  • Network: Ftp, SFtp, Dns, Tcp port, Smtp, Imap, Ssl
  • MongoDB
  • Kafka
  • Identity Server
  • Uri: single uri and uri groups
  • Consul
  • Hangfire
  • SignalR
  • Kubernetes
  • ArangoDB
  • Gremlin

Code Snippet

We need to write extension service class code for AddHealthcheckExtensionService inside Extensions folder of API as below,

public static class HealthcheckExtensionRegistration
    {
        public static IServiceCollection AddHealthcheckExtensionService(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddHealthChecks()
            .AddSqlServer(configuration["ConnectionStrings:IdentityConnectionString"], tags: new[] {
                "db",
                "all"})
            .AddUrlGroup(new Uri("http://localhost:61948/healthz"), tags: new[] {
                "weatherApi",
                "all"
            });

            //adding healthchecks UI
            services.AddHealthChecksUI(opt =>
            {
                opt.SetEvaluationTimeInSeconds(15); //time in seconds between check
                opt.MaximumHistoryEntriesPerEndpoint(60); //maximum history of checks
                opt.SetApiMaxActiveRequests(1); //api requests concurrency
                opt.AddHealthCheckEndpoint("API", "/healthz"); //map health check api
            }).AddSqlServerStorage(configuration["ConnectionStrings:HealthCheckConnectionString"]);
            return services;
        }
    }

Now we need to configure AddHealthcheckExtensionService to service in Program.cs class. Please refer to the below snippet which is required to set up HealthChecks UI.

services.AddHealthcheckExtensionService(Configuration);

By default HealthChecks returns a simple Status Code (200 or 503) without the HealthReport data. If you want that HealthCheck-UI shows the HealthReport data from your HealthCheck you can enable it adding an specific ResponseWriter.

app.MapHealthChecks("/healthz", new HealthCheckOptions
{
    Predicate = _ => true,
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

//map healthcheck ui endpoing - default is /healthchecks-ui/
app.MapHealthChecksUI();

Demo

Demo video to get the clear result view of above implemented module. HealthCheck

Clone this wiki locally