Ramboe.Logging.DependencyInjection
1.0.2
dotnet add package Ramboe.Logging.DependencyInjection --version 1.0.2
NuGet\Install-Package Ramboe.Logging.DependencyInjection -Version 1.0.2
<PackageReference Include="Ramboe.Logging.DependencyInjection" Version="1.0.2" />
<PackageVersion Include="Ramboe.Logging.DependencyInjection" Version="1.0.2" />
<PackageReference Include="Ramboe.Logging.DependencyInjection" />
paket add Ramboe.Logging.DependencyInjection --version 1.0.2
#r "nuget: Ramboe.Logging.DependencyInjection, 1.0.2"
#:package Ramboe.Logging.DependencyInjection@1.0.2
#addin nuget:?package=Ramboe.Logging.DependencyInjection&version=1.0.2
#tool nuget:?package=Ramboe.Logging.DependencyInjection&version=1.0.2
Description
This library is part of the Ramboe.Logging ecosystem. It enables your webservices to write logs into a PostgreSQL database of your choice that can later be read be the Ramboe.Logging.Blazor frontend.
Basic Setup for aspnet (core/7) web services
nuget
Inside your web api, add the nuget package 'Ramboe.Logging.DependencyInjection'
dotnet add package Ramboe.Logging.DependencyInjection --version 1.0.1
on top of that make sure to have the following nuget packages installed
- Serilog.AspNetCore
- Serilog.Sinks.PostgreSQL
Startup
Adjust 'Program.cs' to add the RamboeLogging services to the DI container and use the according middleware:
var builder = WebApplication.CreateBuilder(args);
...
#region ramboe logging
var connectionString = builder.Configuration.GetValue<string>("ConnectionStrings:Logging");
//Make this service log errors into postgres
builder.Services.AddRamboeLogging(connectionString, LogEventLevel.Error);
builder.Host.UseSerilog();
#endregion
...
var app = builder.Build();
#region ramboe logging
//Make this service log errors into postgres
app.UseRamboeExceptionLoggingMiddleware();
#endregion
appSettings
add the ConnectionString for the postgresql database to 'appsettings.json':
{
"ConnectionStrings": {
"Logging": "Host=localhost; Database=postgres; Username=postgres; Password=password"
},
...
}
> CAUTION !
Please be aware, that you can't use the DeveloperExceptionPage and RamboeExceptionLogging at the same time. The DeveloperExceptionPage middleware will suck out the HttpContext and leave it with null values for http body, method, url et cetera.
For that reason please delete the following lines from Program.cs:
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
Track HTTP requests
In case you need to track http requests across your service landscape, you need to modify your HttpClient implementations so they keep track of all requests.
We achieve that by attaching a DelegatingHandler to our http clients that reads the TraceIdentifier from the current HttpContext and adds it as "X-Trace-Id" http header to the outgoing request.
Let's say you'd have a client that sends requests to a 'WebService2'. This would look like this...
Typed HTTP Clients
In case you use typed http clients that look something like this...
public class HttpWebService2Client
{
readonly HttpClient _client;
public HttpWebService2Client(HttpClient client)
{
_client = client;
}
public async Task<string> GetAsync()
{
return await _client.GetStringAsync("");
}
}
you would have to add them to the DI container, just as usual. But instead of using AddHttpClient() from HttpClientFactoryServiceCollectionExtensions namespace, you would simply use AddTypedRamboeLoggingHttpClient() method inside Program.cs:
const string _BASEURL = "https://<url-to-other-service>";
//Add tracable http header to outgoing http requests to track errors across multiple services
builder.Services.AddRamboeLoggingHttpClient<HttpWebService2Client>(_BASEURL );
Named HTTP Clients
In case you prefer named HttpClients you can use the overload of AddRamboeLoggingHttpClient() that also accepts the httpClientName as a parameter:
const string _BASEURL = "https://<url-to-other-service>";
const string _CLIENTNAME = "ramboeHttpClient";
builder.Services
.AddRamboeLoggingHttpClient(_BASEURL, _CLIENTNAME);
Configuration
Since AddRamboeLoggingHttpClient() returns an IHttpClientBuilder, you can configure HttpWebService2Client just as you're used to.
const string _BASEURL = "https://<url-to-other-service>";
builder.Services
.AddRamboeLoggingHttpClient(_BASEURL)
.ConfigureHttpClient(c => c.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json")
));
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.1)
- Serilog.Sinks.PostgreSQL (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.