Create a gist now

Instantly share code, notes, and snippets.

Generic server diagnostics - with WebApi controller sample
using System.Collections.Generic;
namespace Sixeyed.Diagnostics.Models
{
public class DiagnosticCheckCollection
{
public string CollectionName { get; set; }
public bool? Passed { get; set; }
public IEnumerable<DiagnosticCheckResult> Results { get; set; }
public string Notes { get; set; }
}
}
namespace Sixeyed.Diagnostics.Models
{
public class DiagnosticCheckResult
{
public string TestName { get; set; }
public bool Passed { get; set; }
public string Endpoint { get; set; }
public string Notes { get; set; }
}
}
using Sixeyed.Diagnostics.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Http;
namespace Sixeyed.App.Controllers
{
public class DiagnosticsController : ApiController
{
public ServerDiagnostics Get()
{
var diagnostics = new ServerDiagnostics();
try
{
diagnostics.MachineDate = DateTime.Now;
diagnostics.MachineName = Environment.MachineName;
diagnostics.MachineCulture = string.Format("{0} - {1}", CultureInfo.CurrentCulture.DisplayName, CultureInfo.CurrentCulture.Name);
diagnostics.MachineTimeZone = TimeZone.CurrentTimeZone.IsDaylightSavingTime(diagnostics.MachineDate) ? TimeZone.CurrentTimeZone.DaylightName : TimeZone.CurrentTimeZone.StandardName;
diagnostics.DnsHostName = Dns.GetHostName();
diagnostics.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
var ipHost = Dns.GetHostEntry(diagnostics.DnsHostName);
var ipList = new List<string>(ipHost.AddressList.Length);
foreach (var ipAddress in ipHost.AddressList)
{
ipList.Add(ipAddress.ToString());
}
diagnostics.IpAddressList = ipList;
diagnostics.ApplicationName = "Sixeyed App (TODO - your friendly name)";
diagnostics.ApplicationVersionNumber = this.GetType().Assembly.GetName().Version.ToString();
//TODO - add your custom app checks, db access, service connectivity etc.
//var dbChecks = new DiagnosticCheckCollection();
//dbChecks.CollectionName = "Data access";
//dbChecks.Results = GetDbCheckResults();
diagnostics.Checks = new List<DiagnosticCheckCollection>();
var collectionsWithoutResultSet = diagnostics.Checks.Where(x => x.Passed == null).ToList();
collectionsWithoutResultSet.ForEach(x => x.Passed = x.Results.All(y => y.Passed));
SetStatus(diagnostics);
}
catch (Exception ex)
{
diagnostics.Status = string.Format("FAIL - diagnostic check threw error: {0}", ex);
}
return diagnostics;
}
protected virtual void SetStatus(ServerDiagnostics diagnostics)
{
//overall status:
var statusBuilder = new StringBuilder();
foreach (var collection in diagnostics.Checks)
{
if (collection.Passed.HasValue && !collection.Passed.Value)
{
statusBuilder.AppendFormat("{0} checks failed; ", collection.CollectionName);
}
}
diagnostics.Status = statusBuilder.ToString();
if (diagnostics.Status.Length > 0)
{
diagnostics.Status = string.Format("RED - {0}", diagnostics.Status);
}
else
{
diagnostics.Status = "GREEN - service checks all passed";
}
}
}
}
{
"ApplicationName":"Sixeyed App (TODO - your friendly name)",
"ApplicationVersionNumber":"1.0.0.0",
"Status":"GREEN - service checks all passed",
"WorkingDirectory":"c:\\api",
"DnsHostName":"sc-blg-01",
"MachineName":"SC-BLG-01",
"MachineDate":"2014-01-15T21:27:01.7271846+00:00",
"MachineCulture":"English (United Kingdom) - en-GB",
"MachineTimeZone":"GMT Standard Time",
"IpAddressList":["0.0.0.0"],
"Checks":[]
}
using System;
using System.Collections.Generic;
namespace Sixeyed.Diagnostics.Models
{
public class ServerDiagnostics
{
public string ApplicationName { get; set; }
public string ApplicationVersionNumber { get; set; }
public string Status { get; set; }
public string WorkingDirectory { get; set; }
public string DnsHostName { get; set; }
public string MachineName { get; set; }
public DateTime MachineDate { get; set; }
public string MachineCulture { get; set; }
public string MachineTimeZone { get; set; }
public IEnumerable<string> IpAddressList { get; set; }
public IEnumerable<DiagnosticCheckCollection> Checks { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment