Search code examples
azure-web-app-serviceblazor-server-sidehealth-monitoringhealth-check

How do I use Health Checks on Azure App Server


I have set up the health checks in Program.cs for my Blazor server-side application. I'm running it on Azure App Service.

var healthCheckBuilder = builder.Services.AddHealthChecks()
    .AddDbContextCheck<TrackingDbContext>("App Database")
    .AddDbContextCheck<UserDbContext>("Identity Database");
if (!string.IsNullOrEmpty(sendGridKey))
    healthCheckBuilder.AddSendGrid(sendGridKey, name: "SendGrid", failureStatus: HealthStatus.Degraded);
if (!string.IsNullOrEmpty(blobConnStr))
    healthCheckBuilder.AddAzureBlobStorage(blobConnStr,
        name: "Azure Blob Storage", failureStatus: HealthStatus.Degraded);

So what do I do now? I'd like to set it up so Azure will reboot the service and/or spin up a new instance and then kill this instance when needed.

I'd also like, if Azure has it, a page in the dashboard where I can see the history of the health of each service.


Solution

  • Thanks @zori for providing the MSDOC to configure Health Checks.

    I have configured Health Checks through the Portal and also using Code with below steps.

    I have created a Blazor application and configured Health Checks by adding the below code in Program.cs:

    • Refer the article to configure Health Checks in Blazor App programmatically:
    builder.Services.AddHealthChecks();
    app
        .MapHealthChecks("/health", new HealthCheckOptions()
        {
            ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
        });
    

    Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddRazorPages();
    builder.Services.AddServerSideBlazor();
    builder.Services.AddSingleton<WeatherForecastService>();
    builder.Services.AddHealthChecks();
    var app = builder.Build();
    app
        .MapHealthChecks("/health", new HealthCheckOptions()
        {
            ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
        });
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.MapBlazorHub();
    app.MapFallbackToPage("/_Host");
    app.Run();
    

    Local Response for Health Check:

    enter image description here

    Deployed the application Azure App Service

    • Portal Response:

    enter image description here

    Configuring Health Checks in Portal:

    • Navigate to Monitoring=>Health check in the Azure App Service and Enable Health Check and configure the path.

    enter image description here

    • Once the path is specified on your site, App Service will ping it at regular intervals.
    • If it results with the Http status code in the range of 200 to 299, the instance is considered as healthy else it is considered as unhealthy.
    • The instance gets removed from load balancer rotation if the Health check status is unhealthy to avoid load balancer from routing requests to the unhealthy instances.

    Navigate to Instances to check the Server Instance Health Status:

    • Once Health Check is enabled, restart and monitor the status of the application instances through the instances tab.

    enter image description here

    • Go to Metrics to monitor the health status of the application. It displays

    The average health status across the application's instances in the App Service Plan.

    enter image description here