Skip to content
This repository was archived by the owner on Jun 29, 2020. It is now read-only.

Add PostgreSQL health check #10

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Npgsql;

namespace Microsoft.Extensions.HealthChecks
{
Expand Down Expand Up @@ -41,5 +42,27 @@ public static HealthCheckBuilder AddSqlCheck(this HealthCheckBuilder builder, st

return builder;
}

public static HealthCheckBuilder AddPostgreSqlCheck(this HealthCheckBuilder builder, string name, string connectionString)
{
builder.AddCheck($"PostgreSqlCheck({name})", async () =>
{
try
{
using (var connection = new NpgsqlConnection(connectionString))
{
await connection.OpenAsync();

return HealthCheckResult.Healthy($"PostgreSqlCheck({name}): Healthy");
}
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy($"PostgreSqlCheck({name}): Exception during check: {ex.GetType().FullName}");
}
});

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="3.2.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
</ItemGroup>

Expand Down