CSharpEssentials.LoggerHelper.Sink.Postgresql 4.1.4

dotnet add package CSharpEssentials.LoggerHelper.Sink.Postgresql --version 4.1.4
                    
NuGet\Install-Package CSharpEssentials.LoggerHelper.Sink.Postgresql -Version 4.1.4
                    
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="CSharpEssentials.LoggerHelper.Sink.Postgresql" Version="4.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.LoggerHelper.Sink.Postgresql" Version="4.1.4" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.LoggerHelper.Sink.Postgresql" />
                    
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 CSharpEssentials.LoggerHelper.Sink.Postgresql --version 4.1.4
                    
#r "nuget: CSharpEssentials.LoggerHelper.Sink.Postgresql, 4.1.4"
                    
#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 CSharpEssentials.LoggerHelper.Sink.Postgresql@4.1.4
                    
#: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=CSharpEssentials.LoggerHelper.Sink.Postgresql&version=4.1.4
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.LoggerHelper.Sink.Postgresql&version=4.1.4
                    
Install as a Cake Tool

🐘 CSharpEssentials.LoggerHelper.Sink.PostgreSQL

NuGet A flexible PostgreSQL sink for CSharpEssentials.LoggerHelper, designed to store structured logs directly in PostgreSQL with support for custom schemas, JSON fields, and automatic table creation.


Change Log ( 4.1.4 )

  • Connection resilience: if PostgreSQL is unreachable at startup, the sink is skipped gracefully and the service starts normally instead of crashing.
  • Fast-fail timeout: connection attempts during startup are capped at 3 seconds to avoid blocking the ASP.NET Core pipeline.
  • Structured diagnostics: connection errors are classified by category (INFRASTRUCTURE, CREDENTIALS, NETWORK_TIMEOUT, CONFIGURATION, SSL, UNKNOWN) with a human-readable hint field aimed at system administrators.
  • Minimal API endpoint: a new /api/sink-errors endpoint exposes connection errors at runtime β€” see Connection diagnostics endpoint below.

πŸ”₯ Key Features

  • 🐘 Native PostgreSQL integration with auto table creation.
  • πŸ“Š Support for custom schemas and column mappings.
  • πŸ“¦ Handles JSON/JSONB fields for structured data.
  • ⚑ Perfect for analytics, dashboards, and long-term log storage.
  • πŸ”§ Works seamlessly with LoggerHelper’s level-based sink routing.

πŸ“¦ Installation

dotnet add package CSharpEssentials.LoggerHelper.Sink.PostgreSQL

Connection diagnostics endpoint

When PostgreSQL is unreachable at startup the sink is automatically skipped, the service starts normally, and the error is stored in GlobalLogger.Errors. You can expose that information via a Minimal API endpoint directly in your client Program.cs.

Why you need this

Without this endpoint, a connection failure is silent from the outside: the service runs but logs are never written to PostgreSQL. The endpoint lets you β€” or your monitoring system β€” detect the problem immediately without reading logs or restarting the service.

How to add it in your client project

Add the using and the endpoint to your Program.cs as shown below. The endpoint must be registered after app.Build().

// Program.cs
using CSharpEssentials.LoggerHelper; // required for GlobalLogger

var builder = WebApplication.CreateBuilder(args);

// 1. Register LoggerHelper β€” must come before app.Build()
builder.Services.AddloggerConfiguration(builder.Configuration);

// ... your other services ...

var app = builder.Build();

// ... your other middleware ...

// 2. Register the diagnostics endpoint β€” must come after app.Build()
app.MapGet("/api/sink-errors", () =>
{
    var sinkErrors = GlobalLogger.Errors
        .Where(e => e.SinkName == "SelfLog")
        .OrderByDescending(e => e.Timestamp)
        .Select(e => new { e.Timestamp, e.SinkName, e.ErrorMessage })
        .ToList();

    return sinkErrors.Count == 0
        ? Results.Ok(new { status = "ok", message = "No sink errors detected.", errors = sinkErrors })
        : Results.Ok(new { status = "degraded", message = $"{sinkErrors.Count} sink error(s) detected.", errors = sinkErrors });
});

app.Run();

AddloggerConfiguration must always be called before app.Build(). MapGet("/api/sink-errors", ...) must always be called after app.Build().

Response when everything is fine

{
  "status": "ok",
  "message": "No sink errors detected.",
  "errors": []
}

Response when PostgreSQL is unreachable

{
  "status": "degraded",
  "message": "1 sink error(s) detected.",
  "errors": [
    {
      "timestamp": "2026-03-30T09:39:55Z",
      "sinkName": "SelfLog",
      "errorMessage": "[PostgreSQL sink skipped] CATEGORY=INFRASTRUCTURE | TARGET=Host=myhost;Port=5432;Database=mydb;Username=myuser | [NpgsqlException] Exception while reading from stream | InnerException: [IOException] Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. | HINT=The PostgreSQL server is unreachable. Check that the server is running, the host/port are correct, and that no firewall is blocking port 5432. This is NOT an application bug."
    }
  ]
}

Error categories

The CATEGORY and HINT fields inside errorMessage are intended for system administrators, not developers. Each category maps to a specific infrastructure action:

Category Root cause What the sysadmin should check
INFRASTRUCTURE Server down, firewall, wrong host/port PostgreSQL service status, network route, port 5432 open
CREDENTIALS Wrong username/password, pg_hba.conf Connection string credentials, server auth config
NETWORK_TIMEOUT Server overloaded, firewall dropping packets Server load, firewall rules (silent drop vs reject)
CONFIGURATION Database does not exist Database name in the connection string
SSL TLS/certificate mismatch SSL mode in connection string, server certificate
UNKNOWN Unexpected error Escalate full errorMessage to the development team

All categories except UNKNOWN indicate an infrastructure problem, not an application bug.


Demo Project

A full working demo with PostgreSQL integration is available here: CSharpEssentials.Extensions Demo


Product 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 is compatible.  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 is compatible.  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 is compatible.  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
4.1.4 0 3/30/2026
4.1.1 0 3/30/2026
4.1.0 37 3/27/2026
4.0.2 306 9/13/2025
4.0.0 325 8/25/2025
3.1.5 226 6/15/2025
3.1.4 353 6/12/2025
3.1.3 360 6/11/2025
3.1.2 278 6/9/2025
3.1.1 260 6/8/2025
3.0.6 201 6/5/2025
3.0.5 201 6/5/2025
3.0.4 203 6/2/2025
3.0.3 411 6/2/2025
3.0.2 178 6/1/2025
3.0.1 162 5/30/2025
1.0.0 160 5/30/2025