Skip to content

PortiaNet/HealthCheck.Reporter

Repository files navigation

Nuget

What is PortiaNet.HealthCheck.Reporter?

PortiaNet.HealthCheck.Reporter 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)
  • Request Content Length
  • Response Content Length

Installation

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

PM> Install-Package PortiaNet.HealthCheck.Reporter

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

About

The core library of PortiaNet.HealthCheck and the reporter tool

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages