Added Serilog logging, added Elmah error logging, added health checks

This commit is contained in:
2025-06-09 17:09:55 +02:00
parent 60bfcff539
commit ce168cf9e4
10 changed files with 246 additions and 36 deletions

View File

@@ -0,0 +1,44 @@
using System.Collections.ObjectModel;
using Indexer.Models;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Indexer;
public class WorkerHealthCheck : IHealthCheck
{
private readonly WorkerCollection _workerCollection;
public WorkerHealthCheck(WorkerCollection workerCollection)
{
_workerCollection = workerCollection;
}
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default)
{
bool hasDegraded = false;
bool hasUnhealthy = false;
Dictionary<string, HealthStatus> degradedWorkerList = [];
foreach (Worker worker in _workerCollection.Workers)
{
HealthCheckResult workerHealth = worker.HealthCheck();
hasDegraded |= workerHealth.Status == HealthStatus.Degraded;
hasUnhealthy |= workerHealth.Status == HealthStatus.Unhealthy;
if (workerHealth.Status != HealthStatus.Healthy)
{
degradedWorkerList[worker.Name] = workerHealth.Status;
}
}
string degradedWorkerListString = "{" + string.Join(",", [.. degradedWorkerList.Select(kv => '"' + kv.Key + "\": " + kv.Value)]) + "}";
if (hasUnhealthy)
{
return Task.FromResult(
HealthCheckResult.Unhealthy(degradedWorkerListString));
}
else if (hasDegraded)
{
return Task.FromResult(
HealthCheckResult.Degraded(degradedWorkerListString));
}
return Task.FromResult(
HealthCheckResult.Healthy());
}
}