Add health check UI and Serilog dashboard integration
Introduced a `/health-ui` route with a custom HTML-based health check UI, leveraging `HealthCheckService`. Added `GenerateHealthCheckHtml` to dynamically render health check results with Bootstrap styling, auto-refresh, and detailed status reporting. Enabled Serilog self-diagnostics and integrated `app.UseSerilogUi()` for a Serilog log dashboard. Enhanced user experience with navigation links, animations, and responsive design. Improved observability and monitoring capabilities.
This commit is contained in:
@@ -5,10 +5,12 @@ using ECMJobRunner.WebCron.ProfileWorker;
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Serilog;
|
||||
using Serilog.Ui.Core.Extensions;
|
||||
using Serilog.Ui.SqliteDataProvider.Extensions;
|
||||
using Serilog.Ui.Web.Extensions;
|
||||
using System.Text;
|
||||
|
||||
// Enable Serilog self-diagnostics
|
||||
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine($"[SERILOG] {msg}"));
|
||||
@@ -138,6 +140,19 @@ app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
||||
Authorization = new[] { new AllowAllDashboardAuthorizationFilter() }
|
||||
});
|
||||
|
||||
// Add Health Check UI route (accessible outside Hangfire dashboard)
|
||||
app.MapGet("/health-ui", async (HealthCheckService healthCheckService, HttpContext context) =>
|
||||
{
|
||||
var report = await healthCheckService.CheckHealthAsync(context.RequestAborted);
|
||||
|
||||
var html = GenerateHealthCheckHtml(report);
|
||||
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(html);
|
||||
|
||||
return Results.Empty;
|
||||
});
|
||||
|
||||
// Add Serilog.UI Dashboard
|
||||
app.UseSerilogUi();
|
||||
|
||||
@@ -186,3 +201,305 @@ finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
// Helper method to generate health check HTML
|
||||
static string GenerateHealthCheckHtml(HealthReport report)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine(@"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=""UTF-8"">
|
||||
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
|
||||
<title>Health Check - ECMJobRunner</title>
|
||||
<link rel=""stylesheet"" href=""https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"" />
|
||||
<style>
|
||||
body {
|
||||
padding: 40px 20px;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
}
|
||||
.container-fluid { max-width: 1400px; }
|
||||
.status-healthy { color: #198754; }
|
||||
.status-degraded { color: #ffc107; }
|
||||
.status-unhealthy { color: #dc3545; }
|
||||
.badge-healthy { background-color: #198754; }
|
||||
.badge-degraded { background-color: #ffc107; color: #000; }
|
||||
.badge-unhealthy { background-color: #dc3545; }
|
||||
.card {
|
||||
margin-bottom: 20px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
||||
border: none;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
.card:hover { transform: translateY(-4px); box-shadow: 0 6px 12px rgba(0,0,0,0.15); }
|
||||
.card-header {
|
||||
font-weight: 600;
|
||||
border-radius: 12px 12px 0 0 !important;
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
.metric {
|
||||
padding: 12px 0;
|
||||
border-bottom: 1px solid #f0f0f0;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
.metric:last-child { border-bottom: none; }
|
||||
.metric-label { font-weight: 500; color: #666; }
|
||||
.metric-value { color: #333; font-weight: 600; }
|
||||
.refresh-info {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
background: white;
|
||||
padding: 12px 20px;
|
||||
border-radius: 50px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
}
|
||||
.spinner {
|
||||
animation: spin 2s linear infinite;
|
||||
display: inline-block;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 3px solid #f3f3f3;
|
||||
border-top: 3px solid #667eea;
|
||||
border-radius: 50%;
|
||||
}
|
||||
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
||||
.header-card {
|
||||
background: white;
|
||||
border-radius: 16px;
|
||||
padding: 2rem;
|
||||
margin-bottom: 30px;
|
||||
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
||||
}
|
||||
.nav-links {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
padding: 1rem;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.nav-links a {
|
||||
margin-right: 15px;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
let countdown = 10;
|
||||
function updateCountdown() {
|
||||
document.getElementById('countdown').innerText = countdown;
|
||||
countdown--;
|
||||
if (countdown < 0) {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
function autoRefresh() {
|
||||
setInterval(updateCountdown, 1000);
|
||||
}
|
||||
window.onload = autoRefresh;
|
||||
</script>
|
||||
</head>
|
||||
<body>");
|
||||
|
||||
// Navigation
|
||||
sb.Append(@"
|
||||
<div class=""container-fluid"">
|
||||
<div class=""nav-links"">");
|
||||
|
||||
// Append emojis separately (not in verbatim string)
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/hangfire\">🔧 Hangfire Dashboard</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/serilog-ui\">📋 Logs (Serilog.UI)</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/health\">📊 Health API</a>");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <a href=\"/health-ui\">💚 Health UI</a>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(" </div>");
|
||||
|
||||
// Overall Status Header
|
||||
var statusClass = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "status-healthy",
|
||||
HealthStatus.Degraded => "status-degraded",
|
||||
HealthStatus.Unhealthy => "status-unhealthy",
|
||||
_ => ""
|
||||
};
|
||||
|
||||
var badgeClass = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "badge-healthy",
|
||||
HealthStatus.Degraded => "badge-degraded",
|
||||
HealthStatus.Unhealthy => "badge-unhealthy",
|
||||
_ => "badge-secondary"
|
||||
};
|
||||
|
||||
var icon = report.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "✅",
|
||||
HealthStatus.Degraded => "⚠️",
|
||||
HealthStatus.Unhealthy => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""header-card"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<div>
|
||||
<h1 class=""mb-2"">");
|
||||
sb.Append(icon);
|
||||
sb.Append($@" Health Check Status</h1>
|
||||
<p class=""text-muted mb-0"">Last checked: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</p>
|
||||
</div>
|
||||
<div>
|
||||
<h2><span class=""badge {badgeClass} fs-3"">{report.Status}</span></h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""row"">
|
||||
<div class=""col-md-4"">
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-primary text-white"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" 📊 Summary");
|
||||
sb.AppendLine();
|
||||
sb.Append($@" </div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Overall Status:</span>
|
||||
<strong class=""{statusClass}"">{report.Status}</strong>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Total Duration:</span>
|
||||
<span class=""metric-value"">{report.TotalDuration.TotalMilliseconds:F0} ms</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Checks:</span>
|
||||
<span class=""metric-value"">{report.Entries.Count}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""col-md-8"">");
|
||||
|
||||
// Individual Checks
|
||||
foreach (var entry in report.Entries)
|
||||
{
|
||||
var checkStatusClass = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "status-healthy",
|
||||
HealthStatus.Degraded => "status-degraded",
|
||||
HealthStatus.Unhealthy => "status-unhealthy",
|
||||
_ => ""
|
||||
};
|
||||
|
||||
var checkBadgeClass = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "badge-healthy",
|
||||
HealthStatus.Degraded => "badge-degraded",
|
||||
HealthStatus.Unhealthy => "badge-unhealthy",
|
||||
_ => "badge-secondary"
|
||||
};
|
||||
|
||||
var checkIcon = entry.Value.Status switch
|
||||
{
|
||||
HealthStatus.Healthy => "✅",
|
||||
HealthStatus.Degraded => "⚠️",
|
||||
HealthStatus.Unhealthy => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
|
||||
sb.Append($@"
|
||||
<div class=""card"">
|
||||
<div class=""card-header bg-light"">
|
||||
<div class=""d-flex justify-content-between align-items-center"">
|
||||
<span>");
|
||||
sb.Append(checkIcon);
|
||||
sb.Append($@" <strong>{entry.Key}</strong></span>
|
||||
<span class=""badge {checkBadgeClass}"">{entry.Value.Status}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class=""card-body"">
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Description:</span>
|
||||
<span class=""metric-value"">{entry.Value.Description ?? "N/A"}</span>
|
||||
</div>
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Duration:</span>
|
||||
<span class=""metric-value"">{entry.Value.Duration.TotalMilliseconds:F0} ms</span>
|
||||
</div>");
|
||||
|
||||
if (entry.Value.Exception != null)
|
||||
{
|
||||
sb.AppendLine($@"
|
||||
<div class=""metric"">
|
||||
<span class=""metric-label"">Exception:</span>
|
||||
<span class=""metric-value text-danger"" style=""word-break: break-all;"">{System.Net.WebUtility.HtmlEncode(entry.Value.Exception.Message)}</span>
|
||||
</div>");
|
||||
}
|
||||
|
||||
if (entry.Value.Data.Any())
|
||||
{
|
||||
sb.AppendLine(@"
|
||||
<div class=""mt-3"">
|
||||
<strong class=""metric-label"">Details:</strong>
|
||||
<table class=""table table-sm table-borderless mt-2"">");
|
||||
|
||||
foreach (var data in entry.Value.Data)
|
||||
{
|
||||
var value = data.Value?.ToString() ?? "N/A";
|
||||
|
||||
// Format DateTime values
|
||||
if (data.Value is DateTime dt)
|
||||
{
|
||||
value = dt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
else if (data.Value is TimeSpan ts)
|
||||
{
|
||||
value = $"{ts.TotalSeconds:F1}s";
|
||||
}
|
||||
|
||||
sb.AppendLine($@"
|
||||
<tr>
|
||||
<td class=""text-muted"" style=""width: 40%;"">{data.Key}</td>
|
||||
<td><strong>{value}</strong></td>
|
||||
</tr>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</table>
|
||||
</div>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>");
|
||||
}
|
||||
|
||||
sb.AppendLine(@"
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=""refresh-info"">");
|
||||
sb.AppendLine();
|
||||
sb.Append(" <span class=\"spinner\"></span> Auto-refresh in <strong><span id=\"countdown\">10</span>s</strong>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(@" </div>
|
||||
|
||||
</body>
|
||||
</html>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user