Skip to content

Commit

Permalink
Project to help get started with Healthchecks in ASP.NET Core 2.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylindsayni committed Sep 6, 2019
1 parent cda5b75 commit c14243c
Show file tree
Hide file tree
Showing 56 changed files with 39,745 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/Healthcheck/bin
/Healthcheck/obj
/Healthcheck/healthchecksdb
/.vs
25 changes: 25 additions & 0 deletions Healthcheck.sln
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29209.152
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Healthcheck", "Healthcheck\Healthcheck.csproj", "{5DDF3F4D-5BCD-4FF0-9A0D-13D2E72821F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5DDF3F4D-5BCD-4FF0-9A0D-13D2E72821F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DDF3F4D-5BCD-4FF0-9A0D-13D2E72821F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DDF3F4D-5BCD-4FF0-9A0D-13D2E72821F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DDF3F4D-5BCD-4FF0-9A0D-13D2E72821F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {16BADAE4-1CE0-488A-85DD-489EFBD51CDC}
EndGlobalSection
EndGlobal
@@ -0,0 +1,7 @@
{
"ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider",
"Version": "9.1.815.1",
"GettingStartedDocument": {
"Uri": "https://go.microsoft.com/fwlink/?LinkID=798432"
}
}
25 changes: 25 additions & 0 deletions Healthcheck/Controllers/HomeController.cs
@@ -0,0 +1,25 @@
using Healthcheck.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace Healthcheck.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
27 changes: 27 additions & 0 deletions Healthcheck/Healthcheck.csproj
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<ApplicationInsightsResourceId>/subscriptions/**azuresubscriptionkey**/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Healthcheck</ApplicationInsightsResourceId>
<ApplicationInsightsAnnotationResourceId>/subscriptions/**azuresubscriptionkey**/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Healthcheck</ApplicationInsightsAnnotationResourceId>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Publisher.ApplicationInsights" Version="2.2.4" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="2.2.4" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="2.2.34" />
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="2.2.3" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.7.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>


<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions Healthcheck/Models/ErrorViewModel.cs
@@ -0,0 +1,11 @@
using System;

namespace Healthcheck.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
25 changes: 25 additions & 0 deletions Healthcheck/Program.cs
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Healthcheck
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>();
}
}
27 changes: 27 additions & 0 deletions Healthcheck/Properties/launchSettings.json
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59658",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Healthcheck": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
89 changes: 89 additions & 0 deletions Healthcheck/Startup.cs
@@ -0,0 +1,89 @@
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;

namespace Healthcheck
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddHealthChecks()
.AddSqlServer(connectionString: Configuration.GetConnectionString("SqlServerDatabase"),
healthQuery: "SELECT 1;",
name: "Sql Server",
failureStatus: HealthStatus.Degraded)
.AddRedis(redisConnectionString: Configuration.GetConnectionString("RedisCache"),
name: "Redis",
failureStatus: HealthStatus.Degraded)
.AddUrlGroup(new Uri("http://www.google.com"),
name: "Base URL",
failureStatus: HealthStatus.Degraded)
.AddApplicationInsightsPublisher();

services.AddHealthChecksUI(setupSettings: setup =>
{
setup.AddHealthCheckEndpoint("Basic healthcheck", "http://localhost:59658/healthcheck");
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

// 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
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();
app.UseCookiePolicy();

app.UseHealthChecks("/healthcheck", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
})
.UseHealthChecksUI(setup =>
{
setup.AddCustomStylesheet(@"wwwroot\css\dotnet.css");
});

app.UseHealthChecksUI();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
8 changes: 8 additions & 0 deletions Healthcheck/Views/Home/Index.cshtml
@@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
6 changes: 6 additions & 0 deletions Healthcheck/Views/Home/Privacy.cshtml
@@ -0,0 +1,6 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
25 changes: 25 additions & 0 deletions Healthcheck/Views/Shared/Error.cshtml
@@ -0,0 +1,25 @@
@model ErrorViewModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
25 changes: 25 additions & 0 deletions Healthcheck/Views/Shared/_CookieConsentPartial.cshtml
@@ -0,0 +1,25 @@
@using Microsoft.AspNetCore.Http.Features

@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}

@if (showBanner)
{
<div id="cookieConsent" class="alert alert-info alert-dismissible fade show" role="alert">
Use this space to summarize your privacy and cookie use policy. <a asp-area="" asp-controller="Home" asp-action="Privacy">Learn More</a>.
<button type="button" class="accept-policy close" data-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString">
<span aria-hidden="true">Accept</span>
</button>
</div>
<script>
(function () {
var button = document.querySelector("#cookieConsent button[data-cookie-string]");
button.addEventListener("click", function (event) {
document.cookie = button.dataset.cookieString;
}, false);
})();
</script>
}

0 comments on commit c14243c

Please sign in to comment.