Skip to content
This repository was archived by the owner on Jun 29, 2020. It is now read-only.
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
12 changes: 11 additions & 1 deletion HealthChecks.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.27004.2002
MinimumVisualStudioVersion = 15.0.26228.4
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E05DCF88-F916-4B61-A5DC-A8344C9E2429}"
EndProject
Expand All @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.HealthChec
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleHealthChecker.AspNet", "samples\SampleHealthChecker.AspNet\SampleHealthChecker.AspNet.csproj", "{33FB5967-62C7-4230-B515-780EF63F748E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.HealthChecks.MongoDb", "src\Microsoft.Extensions.HealthChecks.MongoDb\Microsoft.Extensions.HealthChecks.MongoDb.csproj", "{17689B4B-7361-417F-B87D-0C8162E0F0C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +65,10 @@ Global
{33FB5967-62C7-4230-B515-780EF63F748E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.Build.0 = Release|Any CPU
{17689B4B-7361-417F-B87D-0C8162E0F0C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17689B4B-7361-417F-B87D-0C8162E0F0C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17689B4B-7361-417F-B87D-0C8162E0F0C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17689B4B-7361-417F-B87D-0C8162E0F0C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -76,5 +82,9 @@ Global
{13BE838A-200B-4A68-8F58-3EA3BE3A1A8A} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
{2AE82E1C-6CE1-4755-A332-FA359B7CCE72} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
{33FB5967-62C7-4230-B515-780EF63F748E} = {E05DCF88-F916-4B61-A5DC-A8344C9E2429}
{17689B4B-7361-417F-B87D-0C8162E0F0C8} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {386F8E33-9F3E-4BC3-B895-A0B43372552F}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Microsoft.Extensions.HealthChecks
{
using System.Linq;
using MongoDB.Driver.Core.Servers;

public static class HealthCheckBuilderMongoDbExtensions
{
public static HealthCheckBuilder AddMongoDbCheck(this HealthCheckBuilder builder, string name, string connectionString, string databaseName)
{
Guard.ArgumentNotNull(nameof(builder), builder);

return AddMongoDbCheck(builder, name, connectionString, databaseName, builder.DefaultCacheDuration);
}

public static HealthCheckBuilder AddMongoDbCheck(this HealthCheckBuilder builder, string name, string connectionString, string databaseName, TimeSpan cacheDuration)
{
builder.AddCheck($"MongoDbCheck({name})", async () =>
{
try
{
var client = new MongoClient(connectionString);
IMongoDatabase database = client.GetDatabase(databaseName);

ServerState serverState = client.Cluster.Description.Servers.FirstOrDefault()?.State
?? ServerState.Disconnected;

if (serverState == ServerState.Disconnected)
{
return HealthCheckResult.Unhealthy($"MongoDbCheck({name}): Unhealthy");
}

BsonDocument result = await database.RunCommandAsync((Command<BsonDocument>) "{ping:1}").ConfigureAwait(false);

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

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\common\Guard.cs" Link="Internal\Guard.cs" />
</ItemGroup>

</Project>