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
                    
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="Ramboe.Logging.DependencyInjection" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ramboe.Logging.DependencyInjection" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Ramboe.Logging.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ramboe.Logging.DependencyInjection --version 1.0.2
                    
#r "nuget: Ramboe.Logging.DependencyInjection, 1.0.2"
                    
#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.
#:package Ramboe.Logging.DependencyInjection@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ramboe.Logging.DependencyInjection&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Ramboe.Logging.DependencyInjection&version=1.0.2
                    
Install as a Cake Tool

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 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. 
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.2 216 10/11/2023
1.0.1 171 9/30/2023
1.0.0 203 9/30/2023 1.0.0 is deprecated because it is no longer maintained.