// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; namespace Microsoft.Extensions.Diagnostics.HealthChecks { /// /// Represents the result of a health check. /// public struct HealthCheckResult { private static readonly IReadOnlyDictionary _emptyReadOnlyDictionary = new Dictionary(); private string _description; private IReadOnlyDictionary _data; /// /// Gets a value indicating the status of the component that was checked. /// public HealthCheckStatus Status { get; } /// /// Gets an representing the exception that was thrown when checking for status (if any). /// /// /// This value is expected to be 'null' if is . /// public Exception Exception { get; } /// /// Gets a human-readable description of the status of the component that was checked. /// public string Description => _description ?? string.Empty; /// /// Gets additional key-value pairs describing the health of the component. /// public IReadOnlyDictionary Data => _data ?? _emptyReadOnlyDictionary; /// /// Creates a new with the specified , , /// , and . /// /// A value indicating the status of the component that was checked. /// An representing the exception that was thrown when checking for status (if any). /// A human-readable description of the status of the component that was checked. /// Additional key-value pairs describing the health of the component. public HealthCheckResult(HealthCheckStatus status, Exception exception, string description, IReadOnlyDictionary data) { if (status == HealthCheckStatus.Unknown) { throw new ArgumentException($"'{nameof(HealthCheckStatus.Unknown)}' is not a valid value for the 'status' parameter.", nameof(status)); } Status = status; Exception = exception; _description = description; _data = data; } /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. public static HealthCheckResult Unhealthy() => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception: null, description: string.Empty, data: null); /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. /// A human-readable description of the status of the component that was checked. public static HealthCheckResult Unhealthy(string description) => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception: null, description: description, data: null); /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. /// A human-readable description of the status of the component that was checked. /// Additional key-value pairs describing the health of the component. public static HealthCheckResult Unhealthy(string description, IReadOnlyDictionary data) => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception: null, description: description, data: data); /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. /// An representing the exception that was thrown when checking for status (if any). public static HealthCheckResult Unhealthy(Exception exception) => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception, description: string.Empty, data: null); /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. /// A human-readable description of the status of the component that was checked. /// An representing the exception that was thrown when checking for status (if any). public static HealthCheckResult Unhealthy(string description, Exception exception) => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception, description, data: null); /// /// Creates a representing an unhealthy component. /// /// A representing an unhealthy component. /// A human-readable description of the status of the component that was checked. /// An representing the exception that was thrown when checking for status (if any). /// Additional key-value pairs describing the health of the component. public static HealthCheckResult Unhealthy(string description, Exception exception, IReadOnlyDictionary data) => new HealthCheckResult(HealthCheckStatus.Unhealthy, exception, description, data); /// /// Creates a representing a healthy component. /// /// A representing a healthy component. public static HealthCheckResult Healthy() => new HealthCheckResult(HealthCheckStatus.Healthy, exception: null, description: string.Empty, data: null); /// /// Creates a representing a healthy component. /// /// A representing a healthy component. /// A human-readable description of the status of the component that was checked. public static HealthCheckResult Healthy(string description) => new HealthCheckResult(HealthCheckStatus.Healthy, exception: null, description: description, data: null); /// /// Creates a representing a healthy component. /// /// A representing a healthy component. /// A human-readable description of the status of the component that was checked. /// Additional key-value pairs describing the health of the component. public static HealthCheckResult Healthy(string description, IReadOnlyDictionary data) => new HealthCheckResult(HealthCheckStatus.Healthy, exception: null, description: description, data: data); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. public static HealthCheckResult Degraded() => new HealthCheckResult(HealthCheckStatus.Degraded, exception: null, description: string.Empty, data: null); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. /// A human-readable description of the status of the component that was checked. public static HealthCheckResult Degraded(string description) => new HealthCheckResult(HealthCheckStatus.Degraded, exception: null, description: description, data: null); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. /// A human-readable description of the status of the component that was checked. /// Additional key-value pairs describing the health of the component. public static HealthCheckResult Degraded(string description, IReadOnlyDictionary data) => new HealthCheckResult(HealthCheckStatus.Degraded, exception: null, description: description, data: data); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. public static HealthCheckResult Degraded(Exception exception) => new HealthCheckResult(HealthCheckStatus.Degraded, exception: null, description: string.Empty, data: null); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. /// A human-readable description of the status of the component that was checked. /// An representing the exception that was thrown when checking for status (if any). public static HealthCheckResult Degraded(string description, Exception exception) => new HealthCheckResult(HealthCheckStatus.Degraded, exception, description, data: null); /// /// Creates a representing a component in a degraded state. /// /// A representing a component in a degraded state. /// A human-readable description of the status of the component that was checked. /// An representing the exception that was thrown when checking for status (if any). /// Additional key-value pairs describing the health of the component. public static HealthCheckResult Degraded(string description, Exception exception, IReadOnlyDictionary data) => new HealthCheckResult(HealthCheckStatus.Degraded, exception, description, data); } }