Posts

Showing posts from 2020

Introducing ASP.NET HealthChecks.Extensions - Conditional Health Checks

Image
ASP.NET Core offers the Health Checks Middleware , in order to check the health state of your .NET Core API. However is not always desirable to run all the registered Health Checks in every context. For example, in environments like the development one, some dependencies might not be available. Other dependencies might be used later during the application lifetime, maybe when their configuration is finished, or after a feature flag is enabled. Would be nice to decide when to run a health check. One possible way is to write the condition in the Startup  class, as bellow: The Redis health check is added only when the configuration setting has the expected value. If the configuration is changed during the application lifetime, then the only way to re-add the Redis health check is by restarting the API, in order to call again the  ConfigureServices  method. Another good reason to switch on or off a health check is when a feature uses one or maybe more dependencies. If that feature is not y

Verify ILogger calls with Moq.ILogger

Moq is by far the most used mocking library for .NET. It provides objects to either mimic real services implementations or to verify if they were called in a certain way by the Service Under Test. Often teams work hard to have meaningful logs in order to help with investigations, to have the possibility to define alarms based on the content of the logs, or even to help with instrumentation. If there is a log entry that resembles as an exception, then a notification might be sent to a Teams/Slack channel, an email, or even to randomly start the alarms on your team's members' cars (joking, don't do that, you know everyone ignores that Slack channel). There are many situations when an investigation ends up with no resolution, and with a promise to the client that the missing logging lines will soon be added. This it translates into development costs, reputation loss, and maybe most importantly having a missed opportunity to learn about the system's behavior in the p