How to check multiple database using AspNetCore.HealthChecks.SqlServer?
Asked Answered
C

1

6

Currently I have checked for one database using these code:

services.AddHealthChecks()
               .AddSqlServer(
                 connectionString: Configuration.GetConnectionString("DefaultConnection"),
                 healthQuery: "SELECT 1;",
                 name: "sql",
                 failureStatus: HealthStatus.Degraded,
                 tags: new string[] { "db", "sql", "sqlserver" }
               );

But how do I check multiple database? I am using .NET Core 3.1 and AspNetCore.HealthChecks.SqlServer Version=3.1.1.

Cacodemon answered 13/8, 2021 at 3:38 Comment(2)
just curious, can you add multiple .AddSqlServer within AddHealthChecks ? otherwise you might think of apply mutliple AddCheck MethodSurface
When I add another .AddSql, it says Duplicate health checks were registered with the name(s): sql (Parameter 'registrations')Cacodemon
R
10

Add another AddSqlServer method but with different name. name must be unique.

services.AddHealthChecks()
    .AddSqlServer(
        connectionString: Configuration.GetConnectionString("DefaultConnection"),
        healthQuery: "SELECT 1;",
        name: "sql",
        failureStatus: HealthStatus.Degraded,
        tags: new string[] { "db", "sql", "sqlserver" })
    .AddSqlServer(
        connectionString: Configuration.GetConnectionString("AnotherConnection"),
        healthQuery: "SELECT 1;",
        name: "sql2",
        failureStatus: HealthStatus.Degraded,
        tags: new string[] { "db", "sql", "sqlserver" }
           );
Ratline answered 13/8, 2021 at 5:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.