Portia.Net.HealthCheck 1.0.1

dotnet add package Portia.Net.HealthCheck --version 1.0.1                
NuGet\Install-Package Portia.Net.HealthCheck -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Portia.Net.HealthCheck" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Portia.Net.HealthCheck --version 1.0.1                
#r "nuget: Portia.Net.HealthCheck, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Portia.Net.HealthCheck as a Cake Addin
#addin nuget:?package=Portia.Net.HealthCheck&version=1.0.1

// Install Portia.Net.HealthCheck as a Cake Tool
#tool nuget:?package=Portia.Net.HealthCheck&version=1.0.1                

<img src="https://user-images.githubusercontent.com/11803924/159634426-45568864-0568-441c-9d0b-a28756f42f9f.png" width="400">

Nuget

What is Portia.Net.HealthCheck?

alternate text is missing from this package README image Portia.Net.HealthCheck is a middleware for .Net web applications (WebAPI and MVC) which collects all required information about the income requests on the server, then delivers them to a service to be saved or analyzed.

The following project has the core functionality of the health check which only collects the information and delivers them to a registered service with the type of IHealthCheckReportService.

The information which this middleware provides are as follows:

  • Client IP Address
  • User Identity (if using authentication middleware)
  • Host Address which has received the request (Usefull for the load balancing)
  • Request Method Type (Get, Put, Push, Patch, Delete)
  • API Path which has been called
  • Query String
  • Full User Agent Information
  • Duration time which the API call took
  • Success Result of the API (To check if API throws any unknown or unhandled exception)

<img src="https://user-images.githubusercontent.com/11803924/159649606-9b56f148-f28f-42f7-be1b-e0d15c41d1a7.png" width="600">

Installation

You can install this tool from Nuget using Package Manager Console:

PM> Install-Package Portia.Net.HealthCheck

How do I get started?

  1. You need a service in your repository to receive the reports from the middleware, then create a simple one and inherit it from IHealthCheckReportService.
public class HealthCheckReportServiceImpl : IHealthCheckReportService
{
    public Task SaveAPICallInformationAsync(RequestDetail requestDetail)
    {
        // Using this method, you can save all collected information in a cloud, local, or third-party database.
        // It is highly recommended to save the tracked information in another database than the main one due to decrease the performance issue
    }
}
  1. The service should be added to the DI in the Configuration section.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<IHealthCheckReportService, HealthCheckReportServiceImpl>();
  1. Add the middleware to the pipeline. Keep in mind to add it after Authentication middleware (if any, otherwise the logged-in user's identity will be always empty). It is recommended to put this middleware as the last one to have accurate API duration information.
var app = builder.Build();
...
app.UseAuthentication();
...
app.UseHealthCheck();
...

How does it work?

When the HealthCheck middleware receives the request, it checks the attributes of the target API, and if it had the HealthCheckIgnoreAttribute attribute then the request won't be tracked.

In the next step, the middleware forwards the request to the next ones in the pipeline. This middleware catches all exceptions which may get thrown by the next middlewares, and if the thrown exception wasn't a subtype of IHealthCheckKnownException, then it will mark the request track entity with an error. Then rethrows the exception to be handled by the previous middlewares (e.g. global exception handler). But before going back to the previous middleware, it sends all gathered information to the SaveAPICallInformationAsync method of the injected IHealthCheckReportService service.

chart drawio

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 450 3/23/2022
1.0.0 414 3/22/2022