Skip to main content

Health Check Configuration

This guide covers all configuration options for Kista repository health checks.

Global Configuration

Configure health checks globally when calling AddKistaRepositories():

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
// Set default timeout for all health checks
options.Timeout = TimeSpan.FromSeconds(10);

// Set failure status (Degraded or Unhealthy)
options.FailureStatus = HealthStatus.Degraded;

// Apply default tags to all repository health checks
options.Tags = ["kista", "repository", "data"];

// Configure startup validation mode
options.StartupValidationMode = StartupValidationMode.Warning;

// Customize health check naming
options.Naming.Template = "Kista:{Driver}:{EntityType}";
});

Configuration Options

PropertyTypeDefaultDescription
TimeoutTimeSpan5 secondsDefault timeout for all health checks
FailureStatusHealthStatusDegradedStatus to report when a health check fails
Tagsstring[]["kista", "repository"]Default tags applied to all health checks
StartupValidationModeStartupValidationModeNoneHow to handle health checks at startup
Naming.Templatestring"Kista:{Driver}:{EntityType}"Template for generating health check names
Naming.NameGeneratorFunc<Type, string>nullCustom function to generate health check names
ExcludedRepositoryTypesHashSet<Type>EmptyRepository types to exclude from health checks
PerRepositoryConfigDictionary<Type, Action>EmptyPer-repository configuration overrides

Driver-Level Configuration

Configure health checks during driver setup:

Entity Framework Core

builder.Services
.AddRepositoryContext()
.UseEntityFramework<MyDbContext>(ef => ef
.WithHealthChecks(options => {
options.Timeout = TimeSpan.FromSeconds(10);
options.TestQuery = true; // Run a lightweight query
options.Tags = ["data", "sql", "ready"];
options.FailureStatus = HealthStatus.Unhealthy;
}));

EntityFrameworkHealthCheckOptions:

PropertyTypeDefaultDescription
TimeoutTimeSpan5 secondsTimeout for the health check
TestQueryboolfalseWhether to run a test query (AnyAsync)
Tagsstring[]["kista", "entityframework", "repository"]Tags for the health check
FailureStatusHealthStatusDegradedFailure status to report

MongoDB

builder.Services
.AddRepositoryContext()
.UseMongoDB<MyMongoContext>(mongo => mongo
.WithHealthChecks(options => {
options.Timeout = TimeSpan.FromSeconds(5);
options.PingTimeout = TimeSpan.FromSeconds(3);
options.Tags = ["data", "mongo", "ready"];
}));

MongoHealthCheckOptions:

PropertyTypeDefaultDescription
TimeoutTimeSpan5 secondsTimeout for the health check
PingTimeoutTimeSpan3 secondsTimeout for the MongoDB ping command
Tagsstring[]["kista", "mongodb", "repository"]Tags for the health check
FailureStatusHealthStatusDegradedFailure status to report

In-Memory

builder.Services
.AddRepositoryContext()
.UseInMemory(inMem => inMem
.WithHealthChecks(options => {
options.Tags = ["cache", "live"];
}));

InMemoryHealthCheckOptions:

PropertyTypeDefaultDescription
TimeoutTimeSpan1 secondTimeout for the health check
Tagsstring[]["kista", "inmemory", "repository"]Tags for the health check
FailureStatusHealthStatusDegradedFailure status to report

Per-Repository Configuration

Override global or driver-level defaults for specific repository types:

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
// Global defaults
options.Timeout = TimeSpan.FromSeconds(5);

// Override for critical entities
options.ConfigureRepository<ProductEntity>(repoOptions => {
repoOptions.Timeout = TimeSpan.FromSeconds(30);
repoOptions.Tags = ["critical", "data", "ready"];
});

// Override for cache entities
options.ConfigureRepository<CacheEntity>(repoOptions => {
repoOptions.Timeout = TimeSpan.FromSeconds(1);
repoOptions.Tags = ["cache", "live"];
});
});

Excluding Repositories

Exclude specific repository types from health checks:

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
// Exclude internal or test repositories
options.ExcludedRepositoryTypes.Add(typeof(InternalCacheEntity));
options.ExcludedRepositoryTypes.Add(typeof(TestEntity));
});

Custom Naming

Using Templates

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
// Available tokens: {Driver}, {EntityType}, {RepositoryType}
options.Naming.Template = "Repository:{EntityType}({Driver})";
});

Example output:

  • "Repository:ProductEntity(EntityFramework)"
  • "Repository:CustomerEntity(MongoDB)"

Using Custom Name Generator

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
options.Naming.NameGenerator = repoType => {
var entityType = Kista.RepositoryRegistrationUtil.GetEntityType(repoType);
var driverType = repoType.Name.Contains("Entity") ? "EntityFramework"
: repoType.Name.Contains("Mongo") ? "MongoDB"
: "InMemory";
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
return $"{entityType?.Name}_{driverType}_{env}";
};
});

Example output:

  • "ProductEntity_EntityFramework_Production"
  • "CustomerEntity_MongoDB_Production"

Configuration from appsettings.json

{
"Kista": {
"HealthChecks": {
"Timeout": "00:00:10",
"FailureStatus": "Degraded",
"Tags": ["kista", "repository"],
"StartupValidationMode": "Warning",
"Naming": {
"Template": "Kista:{Driver}:{EntityType}"
},
"EntityFramework": {
"TestQuery": true,
"Timeout": "00:00:15"
},
"MongoDB": {
"PingTimeout": "00:00:03"
},
"PerRepository": {
"ProductEntity": {
"Timeout": "00:00:30",
"Tags": ["critical", "data"]
}
}
}
}
}
// Load from configuration
builder.Services
.AddRepositoryContext()
.UseEntityFramework<MyDbContext>(ef => ef.WithHealthChecks())
.UseMongoDB<MyMongoContext>();

builder.Services
.AddHealthChecks()
.AddKistaRepositories(
builder.Configuration.GetSection("Kista:HealthChecks"));

Startup Validation

Configure health checks to run at application startup:

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
options.StartupValidationMode = StartupValidationMode.Warning;
});

StartupValidationMode values:

ValueBehavior
NoneNo startup validation (default)
WarningLog warning if health checks fail, but continue startup
FailFastThrow exception and stop startup if health checks fail

Example with FailFast:

builder.Services
.AddHealthChecks()
.AddKistaRepositories(options => {
options.StartupValidationMode = StartupValidationMode.FailFast;
});

var app = builder.Build();
// Application will not start if repository health checks fail
app.Run();

Next Steps