LayeredCraft.Logging.CompactJsonFormatter 1.0.0.2

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

LayeredCraft.Logging.CompactJsonFormatter

Build Status NuGet Downloads

A high-performance Serilog formatter that outputs compact JSON optimized for AWS CloudWatch metric filters and structured log analysis.

Overview

The standard Serilog CompactJsonFormatter uses @ prefixed properties for metadata (like @l for level, @t for timestamp, etc.). However, these @ prefixed properties are not compatible with AWS CloudWatch metric filters, which cannot parse escaped metadata keys.

This formatter solves that problem by replacing the @ prefix with _ (underscore) while maintaining all the performance benefits of the original compact formatter.

Key Features

  • CloudWatch Compatible: Uses _ prefix instead of @ for metadata fields
  • High Performance: Based on the official Serilog compact formatter
  • Single-line JSON: Optimized for log ingestion and analysis
  • Full Serilog Integration: Drop-in replacement for CompactJsonFormatter
  • Trace Support: Includes trace ID and span ID support
  • Multi-targeting: Supports .NET 8.0 and .NET 9.0

Installation

dotnet add package LayeredCraft.Logging.CompactJsonFormatter

Usage

Basic Usage

using LayeredCraft.Logging.CompactJsonFormatter;
using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console(new CompactJsonFormatter())
    .CreateLogger();

Log.Information("Hello, {Name}!", "World");

With Custom Value Formatter

using LayeredCraft.Logging.CompactJsonFormatter;
using Serilog;
using Serilog.Formatting.Json;

var customValueFormatter = new JsonValueFormatter(typeTagName: "$type");

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console(new CompactJsonFormatter(customValueFormatter))
    .CreateLogger();

AWS CloudWatch Integration

using LayeredCraft.Logging.CompactJsonFormatter;
using Serilog;
using Serilog.Sinks.AwsCloudWatch;

Log.Logger = new LoggerConfiguration()
    .WriteTo.AmazonCloudWatch(
        logGroup: "/aws/lambda/my-function",
        region: RegionEndpoint.USEast1,
        formatter: new CompactJsonFormatter())
    .CreateLogger();

Output Format

Standard Serilog CompactJsonFormatter Output

{"@t":"2025-01-14T10:30:00.000Z","@l":"Information","@mt":"Hello, {Name}!","Name":"World"}

LayeredCraft CompactJsonFormatter Output

{"_t":"2025-01-14T10:30:00.000Z","_l":"Information","_mt":"Hello, {Name}!","Name":"World"}

Field Mapping

Serilog Standard LayeredCraft Description
@t _t Timestamp
@l _l Log Level
@mt _mt Message Template
@m _m Rendered Message
@r _r Renderings
@x _x Exception
@tr _tr Trace ID
@sp _sp Span ID

CloudWatch Metric Filters

With the underscore prefix, you can easily create CloudWatch metric filters:

[_l="Error"]
[_l="Warning" && _t > "2025-01-01"]

Project Structure

compact-json-formatter/
├── src/
│   ├── LayeredCraft.Logging.CompactJsonFormatter.csproj
│   └── CompactJsonFormatter.cs
├── test/
│   └── LayeredCraft.Logging.CompactJsonFormatter.Tests.csproj
├── .github/
│   ├── workflows/
│   │   ├── build.yaml
│   │   └── pr-build.yaml
│   └── dependabot.yml
├── Directory.Build.props
├── README.md
├── CLAUDE.md
└── icon.png

Development

Building

dotnet build

Running Tests

dotnet test

Creating NuGet Package

dotnet pack

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for your changes
  5. Ensure all tests pass
  6. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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 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 (1)

Showing the top 1 NuGet packages that depend on LayeredCraft.Logging.CompactJsonFormatter:

Package Downloads
AlexaVoxCraft.MediatR.Lambda

Lambda hosting and middleware integration for Alexa skills using MediatR and AlexaVoxCraft.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.2 190 7/14/2025
1.0.0.1 134 7/14/2025