title: Are there advantages of using .NET Core's middleware "Health checks" over just a ping in a controller's route?
body: <p>I'm reading a &quot;Asp.net Core 3 and Angular 9&quot; book and there is an example usage of .NET Core Health check.
  It's also described on Microsoft website: <a href="https://docs.microsoft.com/en-US/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.0" rel="nofollow noreferrer">https://docs.microsoft.com/en-US/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.0</a>
  I can't find a reason to actually use it over just creating a route in some controller which will ping external addresses.</p>
  <p>Code in a book goes like this:</p>
  <p>Add this in <code>Configure</code> (Startup.cs) method:</p>
  <pre class="lang-cs prettyprint-override"><code>app.UseHealthChecks(&quot;/hc&quot;, new CustomHealthCheckOptions());
  </code></pre>
  <p><code>ConfigureServices</code> method:</p>
  <pre class="lang-cs prettyprint-override"><code>services.AddHealthChecks()
      .AddCheck(&quot;ICMP_01&quot;, new ICMPHealthCheck(&quot;www.ryadel.com&quot;, 100))
      .AddCheck(&quot;ICMP_02&quot;, new ICMPHealthCheck(&quot;www.google.com&quot;, 100))
      .AddCheck(&quot;ICMP_03&quot;, new ICMPHealthCheck(&quot;www.does-notexist.com&quot;, 100));
  </code></pre>
  <p>Create ICMPHealthCheck.cs file:</p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.Extensions.Diagnostics.HealthChecks;
  using System;
  using System.Net.NetworkInformation;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace HealthCheck
  {
      public class ICMPHealthCheck : IHealthCheck
      {
          private string Host { get; set; }
          private int Timeout { get; set; }
  
          public ICMPHealthCheck(string host, int timeout)
          {
              Host = host;
              Timeout = timeout;
          }
  
          public async Task&lt;HealthCheckResult&gt; CheckHealthAsync(
              HealthCheckContext context,
              CancellationToken cancellationToken = default)
          {
              try
              {
                  using (var ping = new Ping())
                  {
                      var reply = await ping.SendPingAsync(Host);
  
                      switch (reply.Status)
                      {
                          case IPStatus.Success:
                              var msg = String.Format(
                                  &quot;IMCP to {0} took {1} ms.&quot;,
                                  Host,
                                  reply.RoundtripTime);
  
                              return (reply.RoundtripTime &gt; Timeout)
                              ? HealthCheckResult.Degraded(msg)
                              : HealthCheckResult.Healthy(msg);
                          default:
                              var err = String.Format(
                                  &quot;IMCP to {0} failed: {1}&quot;,
                                  Host,
                                  reply.Status);
  
                              return HealthCheckResult.Unhealthy(err);
                      }
                  }
              }
              catch (Exception e)
              {
                  var err = String.Format(
                      &quot;IMCP to {0} failed: {1}&quot;,
                      Host,
                      e.Message);
                  return HealthCheckResult.Unhealthy(err);
              }
          }
      }
  }
  </code></pre>
  <p>Create CustomHealthCheckOptions.cs file:</p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.AspNetCore.Diagnostics.HealthChecks;
  using Microsoft.AspNetCore.Http;
  using System.Linq;
  using System.Net.Mime;
  using System.Text.Json;
  namespace HealthCheck
  {
      public class CustomHealthCheckOptions : HealthCheckOptions
      {
          public CustomHealthCheckOptions() : base()
          {
              var jsonSerializerOptions = new JsonSerializerOptions()
              {
                  WriteIndented = true
              };
              ResponseWriter = async (c, r) =&gt;
              {
                  c.Response.ContentType =
                  MediaTypeNames.Application.Json;
                  c.Response.StatusCode = StatusCodes.Status200OK;
                  var result = JsonSerializer.Serialize(new
                  {
                      checks = r.Entries.Select(e =&gt; new
                      {
                          name = e.Key,
                          responseTime = e.Value.Duration.TotalMilliseconds,
                          status = e.Value.Status.ToString(),
                          description = e.Value.Description
                      }),
                      totalStatus = r.Status,
                      totalResponseTime =
                      r.TotalDuration.TotalMilliseconds,
                  }, jsonSerializerOptions);
                  await c.Response.WriteAsync(result);
              };
          }
      }
  }
  </code></pre>
  <p>So it just pings 3 addresses and I can't see advantages of using <code>Microsoft.AspNetCore.Diagnostics.HealthChecks</code> library. Is that wrong example?</p>