コード例 #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var source = (HealthStatus)value;

            var target = new HealthStatusData
            {
                Status    = source.Status.Hummanize(),
                Timestamp = _clock.FormatTimestamp(_clock.UtcDateTime)
            };

            var healthy = source.Results.Where(r => r.Check.Status.IsHealthy())
                          .Select(c => new KeyValuePair <string, string>(c.Name, c.Check.Message))
                          .ToDictionary(pair => pair.Key, pair => pair.Value);

            var unhealthy = source.Results.Where(r => r.Check.Status.IsUnhealthy())
                            .Select(c => new KeyValuePair <string, string>(c.Name, c.Check.Message))
                            .ToDictionary(pair => pair.Key, pair => pair.Value);

            var degraded = source.Results.Where(r => r.Check.Status.IsDegraded())
                           .Select(c => new KeyValuePair <string, string>(c.Name, c.Check.Message))
                           .ToDictionary(pair => pair.Key, pair => pair.Value);

            var ignored = source.Results.Where(r => r.Check.Status.IsIgnored())
                          .Select(c => new KeyValuePair <string, string>(c.Name, c.Check.Message))
                          .ToDictionary(pair => pair.Key, pair => pair.Value);

            if (healthy.Any())
            {
                target.Healthy = healthy;
            }

            if (unhealthy.Any())
            {
                target.Unhealthy = unhealthy;
            }

            if (degraded.Any())
            {
                target.Degraded = degraded;
            }

            if (ignored.Any())
            {
                target.Ignored = ignored;
            }

            serializer.Serialize(writer, target);
        }