Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class CDriveHasMoreThan1GbFreeHealthCheck : IHealthCheck
{
public ValueTask<IHealthCheckResult> CheckAsync(CancellationToken cancellationToken = default(CancellationToken))
{
long freeSpaceinGb = GetTotalFreeSpaceInGb(@"C:\");
CheckStatus status = freeSpaceinGb > 1 ? CheckStatus.Healthy : CheckStatus.Unhealthy;
return new ValueTask<IHealthCheckResult>(HealthCheckResult.FromStatus(status, $"Free Space in GB: {freeSpaceinGb}"));
}
private long GetTotalFreeSpaceInGb(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
{
return drive.TotalFreeSpace / 1024 / 1024 / 1024;
}
}
throw new ArgumentException($"Invalid Drive Name {driveName}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment