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
<PackageReference Include="LayeredCraft.Logging.CompactJsonFormatter" Version="1.0.0.2" />
<PackageVersion Include="LayeredCraft.Logging.CompactJsonFormatter" Version="1.0.0.2" />
<PackageReference Include="LayeredCraft.Logging.CompactJsonFormatter" />
paket add LayeredCraft.Logging.CompactJsonFormatter --version 1.0.0.2
#r "nuget: LayeredCraft.Logging.CompactJsonFormatter, 1.0.0.2"
#:package LayeredCraft.Logging.CompactJsonFormatter@1.0.0.2
#addin nuget:?package=LayeredCraft.Logging.CompactJsonFormatter&version=1.0.0.2
#tool nuget:?package=LayeredCraft.Logging.CompactJsonFormatter&version=1.0.0.2
LayeredCraft.Logging.CompactJsonFormatter
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
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Ensure all tests pass
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- LayeredCraft.StructuredLogging - Structured logging extensions for .NET
Product | Versions 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. |
-
net8.0
- Serilog.Formatting.Compact (>= 3.0.0)
-
net9.0
- Serilog.Formatting.Compact (>= 3.0.0)
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.