コード例 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            var options = new HealthCheckOptions();

            options.ResultStatusCodes[HealthStatus.Unhealthy] = 418;
            options.Predicate             = _ => true;
            options.AllowCachingResponses = false;
            //options.ResponseWriter = () => { };
            options.ResponseWriter = async(c, r) =>
            {
                c.Response.ContentType = "application/json";

                var result = JsonConvert.SerializeObject(new
                {
                    status  = r.Status.ToString(),
                    version = Assembly.GetCallingAssembly().GetName().Version,
                    errors  = r.Entries.Select(e => new { key = e.Key, value = e.Value.Status.ToString() })
                });
                await c.Response.WriteAsync(result);
            };
            app.UseHealthChecks("/hc", options);

            app.UseHealthChecksUI(config => config.UIPath = "/ui");
            app.UseHttpsRedirection();
            app.UseMvc();
        }
コード例 #2
0
 public static IApplicationBuilder UseHealthcheckEndpoint(this IApplicationBuilder builder, HealthCheckOptions options)
 {
     return(builder.UseMiddleware <HealthCheckMiddleware>(options));
 }
コード例 #3
0
 public HealthCheckMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, HealthCheckOptions options)
 {
     _next    = next;
     _logger  = loggerFactory.CreateLogger <HealthCheckMiddleware>();
     _options = options;
 }