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:

    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

    enter image description here

    Configuring Health Checks in Portal:

    enter image description here

    Navigate to Instances to check the Server Instance Health Status:

    enter image description here

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

    enter image description here