امکان بررسی سلامت برنامه در ASP.NET Core 2.2
نویسنده: وحید نصیری
تاریخ: ۱۳۹۷/۱۱/۲۸ ۱۲:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[Route("working")]
public ActionResult Working()
{
using (var connection = new SqlConnection(_connectionString))
{
try
{
connection.Open();
}
catch (SqlException)
{
return new HttpStatusCodeResult(503, "Generic error");
}
}
return new EmptyResult();
} namespace MvcHealthCheckTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddCheck("sql", () =>
{
using (var connection = new SqlConnection(Configuration["connectionString"]))
{
try
{
connection.Open();
}
catch (SqlException)
{
return HealthCheckResult.Unhealthy();
}
}
return HealthCheckResult.Healthy();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHealthChecks("/working"); public class SqlServerHealthCheck : IHealthCheck
{
private readonly IConfiguration _configuration;
public SqlServerHealthCheck(IConfiguration configuration)
{
_configuration = configuration;
}
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
{
using (var connection = new SqlConnection(_configuration["connectionString"]))
{
try
{
connection.Open();
}
catch (SqlException)
{
return Task.FromResult(HealthCheckResult.Unhealthy());
}
}
return Task.FromResult(HealthCheckResult.Healthy());
}
} namespace MvcHealthCheckTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddCheck<SqlServerHealthCheck>("sql"); public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default(CancellationToken))
{
using (var connection = new SqlConnection(_configuration["connectionString"]))
{
try
{
connection.Open();
}
catch (SqlException)
{
return Task.FromResult(new HealthCheckResult(
status: context.Registration.FailureStatus,
description: "It is dead!"));
}
}
return Task.FromResult(HealthCheckResult.Healthy("Healthy as a horse"));
} namespace MvcHealthCheckTest
{
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHealthChecks("/working", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
var result = JsonConvert.SerializeObject(new
{
status = report.Status.ToString(),
errors = report.Entries.Select(e =>
new
{
key = e.Key,
value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
})
});
context.Response.ContentType = MediaTypeNames.Application.Json;
await context.Response.WriteAsync(result);
}
}); public void Configure(IApplicationBuilder app)
{
...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapHealthChecks("/healthz")
.RequireAuthorization(new AuthorizeAttribute(){ Roles = "admin", });
});
} An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
void ConfigureHealthCheck(IServiceCollection services,IConfiguration configuration)
{
services.AddHealthChecks()
.AddApplicationInsightsPublisher()
.AddUrlGroup(new Uri(ssoSettings.BaseAddress),HttpMethod.Get,
name: "Sso Service",
failureStatus: HealthStatus.Degraded,
tags: new[] { "Services" })
.AddSqlServer(getLogDbConnection(configuration),
name: "SqlServer Log Database",
healthQuery: "select 1",
failureStatus: HealthStatus.Degraded,
tags: new[] { "Databases" });
services.AddHealthChecksUI()
.AddInMemoryStorage();
} "connectionString": "Server =IP;Database=LogDb;User ID=sa;Password=xyz;trustServerCertificate=true",
"HealthChecksUI": {
"HealthChecks": [
{
"Name": "Supported Services",
"Uri": "https://Ip server/uaa/health-services"
},
{
"Name": "Supported Databases",
"Uri": "https://Ip server/uaa/health-databases"
}
],
"EvulationTimeInSeconds": 50
}