|
| 1 | +# Health Checks |
| 2 | + |
| 3 | +AspNet Zero has an implementation of health checks and health checks UI. |
| 4 | + |
| 5 | +You can enable or disable health checks and health checks UI. |
| 6 | + |
| 7 | +###### Settings |
| 8 | + |
| 9 | +Health checks settings are located in the `appsettings.json` file |
| 10 | + |
| 11 | +```json |
| 12 | +"HealthChecks": { |
| 13 | + "HealthChecksEnabled": true, //enable/disable all health checks. |
| 14 | + "HealthChecksUI": { |
| 15 | + "HealthChecksUIEnabled": true, //enable/disable health checks ui |
| 16 | + "HealthChecks": [ |
| 17 | + { |
| 18 | + "Name": "MyCompanyName.AbpZeroTemplate.Web.MVC", //your app name |
| 19 | + "Uri": "http://localhost:62114/healthz" /* your_project_url/healthz |
| 20 | + you should change that url before you publish your project*/ |
| 21 | + } |
| 22 | + ], |
| 23 | + "EvaluationTimeOnSeconds": 10, |
| 24 | + "MinimumSecondsBetweenFailureNotifications": 60, |
| 25 | + //"HealthCheckDatabaseConnectionString": "Data Source=[PUT-MY-PATH-HERE]\\healthchecksdb" //-> Optional, default on WebContentRoot, |
| 26 | + //for example, if you use azure you may need to set this connection string |
| 27 | + } |
| 28 | + } |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +> Note: If you enable Health Checks UI, don't forget to change your `healthz` URL before you publish your website. |
| 34 | +
|
| 35 | + |
| 36 | + |
| 37 | +#### Adding new health check |
| 38 | + |
| 39 | +There are a lot of libraries which you can add to your health check easily. To see a full list of libraries and used package in AspNet Zero, see [https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks). Here are some sample package names: |
| 40 | + |
| 41 | +``` |
| 42 | +AspNetCore.HealthChecks.System |
| 43 | +AspNetCore.HealthChecks.Network |
| 44 | +AspNetCore.HealthChecks.SqlServer |
| 45 | +... |
| 46 | +``` |
| 47 | + |
| 48 | +###### Adding your custom health check |
| 49 | + |
| 50 | +To add your own health check, you should create a class inherited from `IHealthCheck`. ( Already implemented health check classes are located under **HealthChecks** folder of `.Application` project.) |
| 51 | + |
| 52 | +```c# |
| 53 | + public class MyCustomHealthCheck : IHealthCheck |
| 54 | + { |
| 55 | + private readonly MyService _myService; |
| 56 | + public AbpZeroTemplateDbContextHealthCheck(MyService myService) |
| 57 | + { |
| 58 | + _myService = myService; |
| 59 | + } |
| 60 | + |
| 61 | + public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) |
| 62 | + { |
| 63 | + if (_myService.CheckSomeThing()) |
| 64 | + { |
| 65 | + return Task.FromResult(HealthCheckResult.Healthy("MyService is healthy.")); |
| 66 | + } |
| 67 | + |
| 68 | + return Task.FromResult(HealthCheckResult.Unhealthy("MyService is unhealthy.")); |
| 69 | + } |
| 70 | + } |
| 71 | +``` |
| 72 | + |
| 73 | +In both cases, you should add your health checks to **HealthCheckBuilder**. HealthCheckBuilder is located in `.Web.Core` project. `( .Web.Core -> HealthCheck -> AbpZeroHealthCheck.cs -> AddAbpZeroHealthCheck )` . |
| 74 | + |
| 75 | +```c# |
| 76 | +public static class AbpZeroHealthCheck |
| 77 | + { |
| 78 | + public static IHealthChecksBuilder AddAbpZeroHealthCheck(this IServiceCollection services) |
| 79 | + { |
| 80 | + var builder = services.AddHealthChecks(); |
| 81 | + builder.AddCheck<AbpZeroTemplateDbContextHealthCheck>("Database Connection"); |
| 82 | + builder.AddCheck<AbpZeroTemplateDbContextUsersHealthCheck>("Database Connection with user check"); |
| 83 | + builder.AddCheck<CacheHealthCheck>("Cache"); |
| 84 | + |
| 85 | + // add your other health checks here |
| 86 | + // builder.AddCheck<MyCustomHealthCheck>("my health check"); |
| 87 | + //... |
| 88 | + return builder; |
| 89 | + } |
| 90 | + } |
| 91 | +``` |
| 92 | + |
| 93 | +After adding your new health check here, you will be able to see its status in JSON and UI automatically. |
| 94 | + |
| 95 | +------ |
| 96 | + |
| 97 | +**Endpoints:** |
| 98 | + |
| 99 | +- *MVC project (Only exists in ASP.NET Core & jQuery version)* |
| 100 | + |
| 101 | + Health checks UI endpoint: http://localhost:62114/healthchecks-ui (if it is enabled) |
| 102 | + |
| 103 | + Health checks JSON result endpoint: http://localhost:62114/healthz (if it is enabled) |
| 104 | + |
| 105 | +- *Host project (Available in ASP.NET Core versions but designed for Angular project)* |
| 106 | + |
| 107 | + Health checks UI endpoint: http://localhost:22742/healthchecks-ui (if it is enabled) |
| 108 | + |
| 109 | + Health checks JSON result endpoint: http://localhost:22742/healthz (if it is enabled) |
| 110 | + |
| 111 | +- *Public Website* |
| 112 | + |
| 113 | + Health checks UI endpoint: http://localhost:45776/healthchecks-ui (if it is enabled) |
| 114 | + |
| 115 | + Health checks JSON result endpoint: http://localhost:45776/healthz (if it is enabled) |
| 116 | + |
| 117 | +see also: |
| 118 | +https://github.com/xabaril/AspNetCore.Diagnostics.HealthChecks |
| 119 | + |
| 120 | +https://docs.docker.com/engine/reference/builder/#healthcheck |
| 121 | + |
| 122 | + |
| 123 | +> Note: If you enable health checks, it will create its dbContext. That's why, when you try to create new migration or update database you should also use `-c [YourProjectName]DbContext` command. |
0 commit comments