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) 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 appOptions = services.GetOptions<AppOptions>(nameof(AppOptions));
var sqlOptions = services.GetOptions<DatabaseOptions>(nameof(DatabaseOptions)); var sqlOptions = services.GetOptions<DatabaseOptions>(nameof(DatabaseOptions));
var rabbitMqOptions = services.GetOptions<RabbitMqOptions>(nameof(RabbitMqOptions)); var rabbitMqOptions = services.GetOptions<RabbitMqOptions>(nameof(RabbitMqOptions));
@ -23,7 +27,9 @@ public static class Extensions
var logOptions = services.GetOptions<LogOptions>(nameof(LogOptions)); var logOptions = services.GetOptions<LogOptions>(nameof(LogOptions));
var healthChecksBuilder = services.AddHealthChecks() 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); .AddElasticsearch(logOptions.Elastic.ElasticServiceUrl);
if (mongoOptions.ConnectionString is not null) if (mongoOptions.ConnectionString is not null)
@ -43,6 +49,10 @@ public static class Extensions
public static WebApplication UseCustomHealthCheck(this WebApplication app) public static WebApplication UseCustomHealthCheck(this WebApplication app)
{ {
var healthOptions = app.Configuration.GetOptions<HealthOptions>(nameof(HealthOptions));
if (!healthOptions.Enabled) return app;
app.UseHealthChecks("/healthz", app.UseHealthChecks("/healthz",
new HealthCheckOptions 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 class ElasticOptions
{ {
public bool Enable { get; set; } public bool Enabled { get; set; }
public string ElasticServiceUrl { get; set; } public string ElasticServiceUrl { get; set; }
public string ElasticSearchIndex { get; set; } public string ElasticSearchIndex { get; set; }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>