Egov.Extensions.Logging 10.0.2

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

Egov.Extensions.Logging

NuGet Version License: MIT

A collection of .NET logging extensions for structured and Elastic-friendly logging. This library provides a fluent API for building complex log events and a custom console formatter that outputs logs in JSON format compatible with Elastic Common Schema (ECS), designed for modern cloud-native and microservices architectures.


Table of Contents


Features

  • Structured Logging: A powerful LoggerEvent class that simplifies adding metadata to logs.
  • Elastic Console Formatter: Custom ConsoleFormatter producing single-line JSON logs (ECS-like).
  • Fluent API: Easily chain properties, correlation IDs, user information, and legal context to your logs.
  • Performance Optimized: Minimal allocations and efficient JSON generation for high-throughput environments.
  • Modern .NET: Built for .NET 10+ leveraging the latest runtime improvements.
  • Easy Integration: Simple extension methods for IServiceCollection, IHostBuilder, and WebApplicationBuilder.

Prerequisites

  • .NET 10.0 or later
  • ASP.NET Core environment (optional, for middleware and web app integration)

Installation

Install the packages via NuGet:

dotnet add package Egov.Extensions.Logging
dotnet add package Egov.Extensions.Logging.Console

Packages

  • Egov.Extensions.Logging: Core library containing the LoggerEvent helper and ILogger extensions.
  • Egov.Extensions.Logging.Console: Custom console formatter for Elastic-compatible JSON output.

Configuration

To use the Elastic console formatter, register it in your Program.cs:

using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add Elastic-compatible console logging
builder.UseElasticConsoleLogging(options =>
{
    options.IncludeScopes = true;
    options.IncludeState = true;
    options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fffZ";
    options.UseUtcTimestamp = true;
});

var app = builder.Build();

Or using the standard ILoggingBuilder:

builder.Logging.AddElasticConsole(options =>
{
    options.IncludeScopes = true;
    options.IncludeState = true;
});

Usage

Using LoggerEvent for Structured Logging

The LoggerEvent class extends ILogger to allow fluent building of metadata-rich logs:

using Microsoft.Extensions.Logging;

public class MyService(ILogger<MyService> logger)
{
    public void ProcessOrder(string orderId, string userId, string correlationId)
    {
        logger.Event()
              .Correlation(correlationId)
              .User(userId)
              .Property("order_id", orderId)
              .Property("status", "processing")
              .LogInformation("Processing order {OrderId}", orderId);
    }

    public void TrackLegalContext(string entity, string reason, string basis)
    {
        logger.Event()
              .LegalEntity(entity)
              .LegalReason(reason, basis)
              .LogWarning("Accessing restricted data");
    }
}

Elastic Console Output Example

The output is a single-line JSON designed for log collectors like Filebeat or Logstash:

{
  "event_time": "2026-04-10 17:26:00.123Z",
  "event_level": "info",
  "event_source": "MyNamespace.MyService",
  "event_message": "Processing order ORD-12345",
  "event_id": 0,
  "event_correlation": "corr-456",
  "user": "user-789",
  "order_id": "ORD-12345",
  "status": "processing"
}

Testing

The solution includes a comprehensive test suite using xUnit and Moq.

Running the tests

dotnet test src/Egov.Extensions.Logging.sln

Contributing

Contributions are welcome! Please read CONTRIBUTING.md for guidelines on how to get started.


Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.


AI Assistance

This repository contains an AGENTS.md file with instructions and context for AI coding agents to assist in development, ensuring consistency in code style and project structure.


License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET 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.
  • net10.0

    • No dependencies.

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
10.0.2 122 4/10/2026
10.0.0 104 4/10/2026