PortiaNet.HealthCheck.Writer.HTTP
1.0.2
See the version list below for details.
dotnet add package PortiaNet.HealthCheck.Writer.HTTP --version 1.0.2
NuGet\Install-Package PortiaNet.HealthCheck.Writer.HTTP -Version 1.0.2
<PackageReference Include="PortiaNet.HealthCheck.Writer.HTTP" Version="1.0.2" />
paket add PortiaNet.HealthCheck.Writer.HTTP --version 1.0.2
#r "nuget: PortiaNet.HealthCheck.Writer.HTTP, 1.0.2"
// Install PortiaNet.HealthCheck.Writer.HTTP as a Cake Addin #addin nuget:?package=PortiaNet.HealthCheck.Writer.HTTP&version=1.0.2 // Install PortiaNet.HealthCheck.Writer.HTTP as a Cake Tool #tool nuget:?package=PortiaNet.HealthCheck.Writer.HTTP&version=1.0.2
<img src="https://github.com/PortiaNet/HealthCheck.Reporter/blob/master/Assets/banner.png" width="400">
This library is the writer for the PortiaNet.HealthCheck.Reporter and pushes the API call reports to a specific URL using HTTP methods.
Installation
You can install this tool from Nuget using Package Manager Console:
PM> Install-Package PortiaNet.HealthCheck.Writer.HTTP
How do I get started?
After adding the HealthCheck middleware to the application pipeline, you need to add this library to DI and configure it as you prefer.
using PortiaNet.HealthCheck.Writer;
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddHTTPWriter(options =>
{
options.ListenerAddress = new Uri("Target API to push the report");
options.MuteOnError = false;
options.AuthenticationType = AuthenticationType.None;
options.NodeName = "Main Node without authentication";
});
builder.Services.AddHTTPWriter(options =>
{
options.ListenerAddress = new Uri("Target API to push the report");
options.MuteOnError = false;
options.AuthenticationType = AuthenticationType.StaticBearerToken;
options.NodeName = "Main Node by Static Bearer Token";
options.AuthenticationConfig = new StaticBearerTokenAuthentication
{
Token = "A very hard and long super secret token!!!"
};
});
builder.Services.AddHTTPWriter(options =>
{
options.ListenerAddress = new Uri("Target API to push the report");
options.MuteOnError = false;
options.AuthenticationType = AuthenticationType.ClientSecretBearerToken;
options.NodeName = "Main Node by ClientSecret";
options.AuthenticationConfig = new ClientSecretBearerTokenAuthentication
{
AuthenticationAPIPath = new Uri("Authentication API"),
ClientSecret = "***ClientSecretText&&&"
};
});
builder.Services.AddHTTPWriter(options =>
{
options.ListenerAddress = new Uri("Target API to push the report");
options.MuteOnError = false;
options.AuthenticationType = AuthenticationType.UsernamePasswordBearerToken;
options.NodeName = "Main Node by Username and Password";
options.AuthenticationConfig = new UsernamePasswordBearerTokenAuthentication
{
AuthenticationAPIPath = new Uri("Authentication API"),
Username = "TestUser",
Password = "P@ssvor3d"
};
});
...
This library supports 4 types of communication with the target service:
- Without Authentication
In the first method, the library posts the report to the target API without setting the Authorization header.
- Static Bearer Token Authentication (Doesn't need to call authentication API)
In the second method, the library sets the Authorization header with the Bearer prefix and the exact value of the specified token for all API calls.
- Client Secret Authentication
In the third method, the library posts the specified Client Secret to the target authentication API to receive the bearer token. after receiving the token, it sets the Authorization header with the Bearer prefix and the exact value of the received token for all API calls. In this case, you need to have a POST API endpoint to receive the Client Secret from the request body and send the token back as the method result with a 200 status code.
[HttpPost]
public IActionResult AuthenticateByClientSecret([FromBody]string clientSecret)
{
if(string.IsNullOrEmpty(clientSecret))
return Unauthorized();
return Ok("Bearer Token");
}
- Username and Password Authentication
In the fourth method, the library posts the specified Username and Password to the target authentication API to receive the bearer token. after receiving the token, it sets the Authorization header with the Bearer prefix and the exact value of the received token for all API calls. In this case, you need to have a POST API endpoint to receive the credentials from the request body and send the token back as the method result with a 200 status code.
public class UsernamePasswordModel
{
public string? Username { get; set; }
public string? Password { get; set; }
}
[HttpPost]
public IActionResult AuthenticateByUsernamePassword([FromBody] UsernamePasswordModel credential)
{
if (credential == null || credential.Username != "Username" && credential.Password != "Password")
return Unauthorized();
return Ok("Bearer Token");
}
After specifying the authentication method, you need to configure the target API to receive and save the reports. This API should be a POST method and receives the Reporter model.
public class RequestDetail
{
public string? IpAddress { get; set; }
public string? Username { get; set; }
public string? Host { get; set; }
public string? Method { get; set; }
public string? Path { get; set; }
public string? QueryString { get; set; }
public string? UserAgent { get; set; }
public double Duration { get; set; }
public bool HadError { get; set; }
public string? NodeName { get; set; }
public DateTime EventDateTime { get; set; }
}
[HttpPost]
public IActionResult SaveReport([FromBody] RequestDetail report)
{
Debugger.Log(0, null, JsonSerializer.Serialize(report));
return Ok();
}
There is an option in the configuration to dump a set of logs instead of sending them one by one. To use this functionality, you need to set the BulkDataDumpingEnabled to true. Then the report writer will create a set of records based on the limitation has been set by DataDumpingSize and sends it in one package. In this case, the target API should receive a set of RequestDetail instead of an individual item. The following sample shows the configuration for the bulk data dumping:
builder.Services.AddHTTPWriter(options =>
{
options.ListenerAddress = new Uri("Target API to push the report");
options.MuteOnError = false;
options.AuthenticationType = AuthenticationType.None;
options.NodeName = "Main Node Bulk Reprot";
options.BulkDataDumpingEnabled = true;
options.DataDumpingSize = 100;
});
And the following sample shows an instance of the report listener:
[HttpPost]
public IActionResult SaveBulkReport([FromBody] List<RequestDetail> report)
{
Debugger.Log(0, null, JsonSerializer.Serialize(report));
return Ok();
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- PortiaNet.HealthCheck.Reporter (>= 1.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.