feat: Add health-options

This commit is contained in:
meysamhadeli 2023-01-03 22:59:47 +03:30
parent edd50ac41a
commit 624194bb01
13 changed files with 74 additions and 46 deletions

View File

@ -16,6 +16,10 @@ public static class Extensions
{
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services)
{
var healthOptions = services.GetOptions<HealthOptions>(nameof(HealthOptions));
if (!healthOptions.Enabled) return services;
var appOptions = services.GetOptions<AppOptions>(nameof(AppOptions));
var sqlOptions = services.GetOptions<DatabaseOptions>(nameof(DatabaseOptions));
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>(nameof(RabbitMqOptions));
@ -23,7 +27,9 @@ public static class Extensions
var logOptions = services.GetOptions<LogOptions>(nameof(LogOptions));
var healthChecksBuilder = services.AddHealthChecks()
.AddRabbitMQ(rabbitConnectionString: $"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}")
.AddRabbitMQ(
rabbitConnectionString:
$"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}")
.AddElasticsearch(logOptions.Elastic.ElasticServiceUrl);
if (mongoOptions.ConnectionString is not null)
@ -43,6 +49,10 @@ public static class Extensions
public static WebApplication UseCustomHealthCheck(this WebApplication app)
{
var healthOptions = app.Configuration.GetOptions<HealthOptions>(nameof(HealthOptions));
if (!healthOptions.Enabled) return app;
app.UseHealthChecks("/healthz",
new HealthCheckOptions
{

View File

@ -0,0 +1,6 @@
namespace BuildingBlocks.HealthCheck;
public class HealthOptions
{
public bool Enabled { get; set; } = true;
}

View File

@ -2,7 +2,7 @@
public class ElasticOptions
{
public bool Enable { get; set; }
public bool Enabled { get; set; }
public string ElasticServiceUrl { get; set; }
public string ElasticSearchIndex { get; set; }
}
}

View File

@ -38,7 +38,7 @@ namespace BuildingBlocks.Logging
.Enrich.FromLogContext()
.ReadFrom.Configuration(context.Configuration);
if (logOptions.Elastic is { Enable: true })
if (logOptions.Elastic is { Enabled: true })
{
loggerConfiguration.WriteTo.Elasticsearch(
new ElasticsearchSinkOptions(new Uri(logOptions.Elastic.ElasticServiceUrl))
@ -49,7 +49,7 @@ namespace BuildingBlocks.Logging
}
if (logOptions?.Sentry is {Enable: true})
if (logOptions?.Sentry is {Enabled: true})
{
var minimumBreadcrumbLevel = Enum.TryParse<LogEventLevel>(logOptions.Level, true, out var minBreadcrumbLevel)
? minBreadcrumbLevel
@ -67,7 +67,7 @@ namespace BuildingBlocks.Logging
});
}
if (logOptions.File is { Enable: true })
if (logOptions.File is { Enabled: true })
{
var root = env.ContentRootPath;
Directory.CreateDirectory(Path.Combine(root, "logs"));

View File

@ -2,7 +2,7 @@
public class FileOptions
{
public bool Enable { get; set; }
public bool Enabled { get; set; }
public string Path { get; set; }
public string Interval { get; set; }
}
}

View File

@ -2,7 +2,7 @@
public class SentryOptions
{
public bool Enable { get; set; }
public bool Enabled { get; set; }
public string Dsn { get; set; }
public string MinimumBreadcrumbLevel { get; set; }
public string MinimumEventLevel { get; set; }

View File

@ -6,19 +6,19 @@
"Level": "information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
"Elastic": {
"Enable": true,
"Enabled": true,
"ElasticServiceUrl": "http://localhost:9200"
},
"File": {
"enable": false,
"path": "logs/logs.txt",
"interval": "day"
"Enabled": false,
"Path": "logs/logs.txt",
"Interval": "day"
},
"Sentry": {
"enable": false,
"dsn": "",
"minimumBreadcrumbLevel": "information",
"minimumEventLevel":"error"
"Enabled": false,
"Dsn": "",
"MinimumBreadcrumbLevel": "information",
"MinimumEventLevel":"error"
}
},
"Jwt": {
@ -43,6 +43,9 @@
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "booking-db"
},
"HealthOptions": {
"Enabled": false
},
"PersistMessageOptions": {
"Interval": 30,
"Enabled": true,

View File

@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.Tools" Version="2.50.0">
<PackageReference Include="Grpc.Tools" Version="2.51.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -6,19 +6,19 @@
"Level": "information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
"Elastic": {
"Enable": true,
"Enabled": true,
"ElasticServiceUrl": "http://localhost:9200"
},
"File": {
"enable": false,
"path": "logs/logs.txt",
"interval": "day"
"Enabled": false,
"Path": "logs/logs.txt",
"Interval": "day"
},
"Sentry": {
"enable": false,
"dsn": "",
"minimumBreadcrumbLevel": "information",
"minimumEventLevel":"error"
"Enabled": false,
"Dsn": "",
"MinimumBreadcrumbLevel": "information",
"MinimumEventLevel": "error"
}
},
"DatabaseOptions": {
@ -44,5 +44,8 @@
"Enabled": true,
"ConnectionString": "Server=.\\sqlexpress;Database=PersistMessageDB;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
},
"HealthOptions": {
"Enabled": false
},
"AllowedHosts": "*"
}

View File

@ -5,9 +5,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.49.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.49.0" />
<PackageReference Include="Grpc.Tools" Version="2.50.0">
<PackageReference Include="Grpc.AspNetCore" Version="2.51.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.51.0" />
<PackageReference Include="Grpc.Tools" Version="2.51.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -20,21 +20,24 @@
"Level": "information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
"Elastic": {
"Enable": true,
"Enabled": true,
"ElasticServiceUrl": "http://localhost:9200"
},
"File": {
"enable": false,
"path": "logs/logs.txt",
"interval": "day"
"Enabled": false,
"Path": "logs/logs.txt",
"Interval": "day"
},
"Sentry": {
"enable": false,
"dsn": "",
"minimumBreadcrumbLevel": "information",
"minimumEventLevel":"error"
"Enabled": false,
"Dsn": "",
"MinimumBreadcrumbLevel": "information",
"MinimumEventLevel":"error"
}
},
"HealthOptions": {
"Enabled": false
},
"PersistMessageOptions": {
"Interval": 30,
"Enabled": true,

View File

@ -24,21 +24,24 @@
"Level": "information",
"LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}",
"Elastic": {
"Enable": true,
"Enabled": true,
"ElasticServiceUrl": "http://localhost:9200"
},
"File": {
"enable": false,
"path": "logs/logs.txt",
"interval": "day"
"Enabled": false,
"Path": "logs/logs.txt",
"Interval": "day"
},
"Sentry": {
"enable": false,
"dsn": "",
"minimumBreadcrumbLevel": "information",
"minimumEventLevel":"error"
"Enabled": false,
"Dsn": "",
"MinimumBreadcrumbLevel": "information",
"MinimumEventLevel":"error"
}
},
"HealthOptions": {
"Enabled": false
},
"PersistMessageOptions": {
"Interval": 30,
"Enabled": true,

View File

@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.49.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.51.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>