Skip to content

Instantly share code, notes, and snippets.

@scottsauber
scottsauber / Startup.cs
Last active May 22, 2017 05:46
Custom Health Checks
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<CDriveHasMoreThan1GbFreeHealthCheck>();
services.AddHealthChecks(checks =>
{
checks.AddCheck<CDriveHasMoreThan1GbFreeHealthCheck>("C Drive has more than 1 GB Free");
});
services.AddMvc();
public class CDriveHasMoreThan1GbFreeHealthCheck : IHealthCheck
{
public ValueTask<IHealthCheckResult> CheckAsync(CancellationToken cancellationToken = default(CancellationToken))
{
long freeSpaceinGb = GetTotalFreeSpaceInGb(@"C:\");
CheckStatus status = freeSpaceinGb > 1 ? CheckStatus.Healthy : CheckStatus.Unhealthy;
return new ValueTask<IHealthCheckResult>(HealthCheckResult.FromStatus(status, $"Free Space in GB: {freeSpaceinGb}"));
}
[Route("api/[controller]")]
public class HealthCheckController : Controller
{
private readonly IHealthCheckService _healthCheckService;
public HealthCheckController(IHealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
}
<environment names="Development">
<h1>This only renders in the Development Environment!</h1>
</environment>
<environment names="Staging,Production">
<h1>This only renders in the Staging and Production Environments!</h1>
</environment>
<environment include="Development, Staging" exclude="Development">
<h1>This will not show in Development or Production, but it will show in Staging.</h1>
</environment>
<environment include="Development">
<h1>This only renders in the Development Environment</h1>
</environment>
<environment exclude="Development">
<h1>This renders in anything except the Development Environment (Staging, Production, or any custom environment).</h1>
</environment>
@scottsauber
scottsauber / Startup.cs
Last active July 20, 2017 04:06
Environments
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// if we're in Development
if (env.IsDevelopment())
{
// Show the Light Blue Screen of Death if there's an unhandled exception (RIP Yellow Screen of Death of ASP.NET 4)
app.UseDeveloperExceptionPage();
}
else
{
public class FeatureViewLocationRazorViewEngine : RazorViewEngine
{
public FeatureViewLocationRazorViewEngine()
{
var featureFolderViewLocationFormats = new[]
{
"~/Features/{1}/{0}.cshtml",
"~/Features/Shared/{0}.cshtml",
};
@scottsauber
scottsauber / FeatureLocationExpander.cs
Last active July 26, 2017 21:04
ASP.NET Core View Intellisense for Feature Folders
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Razor;
using JetBrains.Annotations; // Install JetBrains.Annotations NuGet package
[assembly: AspMvcViewLocationFormat("~/Features/{1}/{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("~/Features/Shared/{0}.cshtml")]
namespace WebApplication6.StartupCustomizations
{
public class FeatureLocationExpander : IViewLocationExpander
{
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />