TNTKronosys.Monitoring 1.1.0

TNTKronosys.Monitoring

A lightweight ASP.NET Core library for exposing standardized health polling endpoints. When integrated into your project, it provides GET /api/health/polling which returns health data in the shape expected by the main TNTMonitoring-Backend polling service.

Features

  • Standardized Health Endpoint — GET /api/health/polling exposed automatically
  • Extensible Health Checks — Implement IHealthDataProvider to define your application's health logic
  • Dependency Tracking — Report status of databases, external APIs, caches, and other dependencies
  • Performance Metrics — Include response time, version, and detailed status messages
  • Simple Integration — Two lines of code in Program.cs to enable full functionality

Quick Start

1. Add the NuGet Package

(Add TNTKronosys.Monitoring to your project)

2. Implement IHealthDataProvider

public class YourProjectHealthProvider : IHealthDataProvider
{
	private readonly YourDbContext _dbContext;
	private readonly IHttpClientFactory _httpClientFactory;

	public YourProjectHealthProvider(
		YourDbContext dbContext, 
		IHttpClientFactory httpClientFactory)
	{
		_dbContext = dbContext;
		_httpClientFactory = httpClientFactory;
	}

	public async Task<PollingHealthResponse> GetHealthAsync()
	{
		var startTime = DateTime.UtcNow;
		var dependencies = new List<DependencyStatus>();
		var overallStatus = "Healthy";

		// Check Database
		try
		{
			await _dbContext.Database.ExecuteSqlRawAsync("SELECT 1");
			dependencies.Add(new DependencyStatus 
			{ 
				Name = "Database", 
				Status = "Healthy" 
			});
		}
		catch (Exception ex)
		{
			dependencies.Add(new DependencyStatus 
			{ 
				Name = "Database", 
				Status = "Unhealthy", 
				Details = "Connection check failed" 
			});
			overallStatus = "Unhealthy";
		}

		// Add more dependency checks as needed...

		var responseTime = (DateTime.UtcNow - startTime).TotalMilliseconds;

		return new PollingHealthResponse
		{
			Status = overallStatus,
			ResponseTime = responseTime,
			Timestamp = DateTime.UtcNow,
			Dependencies = dependencies,
			Message = overallStatus == "Healthy" 
				? "All systems operational" 
				: "System experiencing issues"
		};
	}
}

3. Register in Program.cs

// Register your health provider
builder.Services.AddScoped<IHealthDataProvider, YourProjectHealthProvider>();

// Ensure these are present (if not already)
builder.Services.AddControllers();
builder.Services.AddHttpClient();

// Register the health polling library
builder.Services.AddHealthPolling();

var app = builder.Build();

// Ensure this is present (if not already)
app.MapControllers();

app.Run();

4. Done!

Your API now exposes GET /api/health/polling. The main TNTMonitoring-Backend can call this endpoint to get your application's health status.

Response Format

{
  "status": "Healthy",
  "responseTime": 45.23,
  "timestamp": "2024-01-15T10:30:45.1234567Z",
  "dependencies": [
	{
	  "name": "Database",
	  "status": "Healthy",
	  "details": null
	}
  ],
  "message": "All systems operational"
}

Response Behaviour

  • The endpoint always returns HTTP 200 when the check completes, including when the application is unhealthy. The health state is carried in the body.
  • status is the source of truth: Healthy, Degraded or Unhealthy. The TNTMonitoring polling service marks an endpoint up for Healthy and Degraded, and down for Unhealthy or a non-success HTTP response.
  • version is returned at the top level. Set Version in your provider (for example from your assembly's informational version) and set the endpoint's version field path to version in TNTMonitoring.
  • If GetHealthAsync throws, the endpoint logs the exception and returns Unhealthy with no dependencies.
  • The endpoint is anonymous. Do not put exception messages, connection strings or hostnames in Details; use short, generic messages and log the full exception yourself.

Configuration

// Default: GET /api/health/polling
builder.Services.AddHealthPolling();

// Custom route prefix: GET /monitoring/health/polling
builder.Services.AddHealthPolling(options => options.Route = "monitoring/health");
  • Route defaults to api/health. Leading and trailing slashes are ignored, and an empty route throws an ArgumentException.
  • AddHealthPolling() can be called before or after registering IHealthDataProvider. The provider is resolved when the application starts, and startup fails with a clear message if it cannot be resolved.

Core Types

PollingHealthResponse

The standardized response returned by /api/health/polling:

  • Status (string) — "Healthy", "Degraded", or "Unhealthy"
  • ResponseTime (double) — milliseconds for the health check
  • Timestamp (DateTime) — UTC when the check was performed
  • Dependencies (List) — status of each dependency
  • Message (string?) — optional summary message

DependencyStatus

Represents the health of a single dependency:

  • Name (string) — e.g., "Database", "ExternalAPI"
  • Status (string) — "Healthy", "Degraded", or "Unhealthy"
  • Details (string?) — optional error message or details

IHealthDataProvider

Interface you implement to provide health data:

Task<PollingHealthResponse> GetHealthAsync();

Documentation

For detailed implementation examples including PostgreSQL, MongoDB, hybrid databases, and external API integration, see IMPLEMENTATION_GUIDE.md.

Architecture

The library follows a simple, extensible architecture:

IHealthDataProvider (your implementation)
		↓
   GetHealthAsync()
		↓
PollingHealthResponse (standardized DTO)
		↓
HealthPollingController
		↓
GET /api/health/polling

Each consuming application:

  1. Implements IHealthDataProvider with their own health logic
  2. Registers it in their Program.cs
  3. Calls AddHealthPolling() to enable the endpoint
  4. The main backend polls the endpoint for standardized health data

Building & Testing

Build the Library

dotnet build TNTKronosys.Monitoring/TNTKronosys.Monitoring.csproj

Test the Endpoint Locally

curl -X GET "http://localhost:5000/api/health/polling"

Integrate into Your Project

// In Program.cs
builder.Services.AddScoped<IHealthDataProvider, YourHealthProvider>();
builder.Services.AddHealthPolling();

Dependencies

  • ASP.NET Core shared framework (Microsoft.AspNetCore.App) — referenced via FrameworkReference

License

See repository for license information.

Support

For issues or questions, refer to the main TNTMonitoring-Backend repository or contact the development team.

No packages depend on TNTKronosys.Monitoring.

.NET 8.0

  • No dependencies.

Version Downloads Last updated
1.1.0 62 10/16/2026
1.0.0 5 10/15/2026