Created
April 28, 2020 07:56
-
-
Save Mitya88/b45d25be86ea76514a794e7ad18fbcd3 to your computer and use it in GitHub Desktop.
Sitecore healthcheck custom implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// MongoDb sample health check | |
/// </summary> | |
/// <seealso cref="Healthcheck.Service.Customization.CustomHealthcheckRun" /> | |
public class MongoDbComponentHealth : CustomHealthcheckRun | |
{ | |
/// <summary> | |
/// Does the healthcheck. | |
/// </summary> | |
/// <param name="parameters">The parameters.</param> | |
/// <returns> | |
/// The healthcheck result | |
/// </returns> | |
public override CustomHealthcheckResult DoHealthcheck(NameValueCollection parameters) | |
{ | |
var connectionString = parameters["ConnectionString"]; | |
var result = new CustomHealthcheckResult | |
{ | |
HealthyMessage = "Connection is ok", | |
Status = HealthcheckStatus.Healthy | |
}; | |
try | |
{ | |
if (string.IsNullOrEmpty(connectionString)) | |
{ | |
result.Status = HealthcheckStatus.Warning; | |
result.ErrorMessage = "Connectionstring is not provided in 'ConnectionString' param"; | |
return result; | |
} | |
var client = new MongoClient(connectionString); | |
if (client.Cluster.Description.State != MongoDB.Driver.Core.Clusters.ClusterState.Connected) | |
{ | |
result.Status = HealthcheckStatus.Error; | |
result.ErrorMessage = "State is disconnected"; | |
} | |
} | |
catch (Exception ex) | |
{ | |
result.Status = HealthcheckStatus.Error; | |
result.Exception = ex; | |
result.ErrorMessage = ex.Message; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment