Serilog.Formatting.Compact 2.0.0-dev-00961
Serilog.Formatting.Compact
A simple, compact JSON-based event format for Serilog. CompactJsonFormatter
significantly reduces the byte count of small log events when compared with Serilog's default JsonFormatter
, while remaining human-readable. It achieves this through shorter built-in property names, a leaner format, and by excluding redundant information.
Sample
A simple Hello, {User}
event.
{"@t":"2016-06-07T03:44:57.8532799Z","@mt":"Hello, {User}","User":"nblumhardt"}
Getting started
Install from NuGet:
Install-Package Serilog.Formatting.Compact
The formatter is used in conjunction with sinks that accept ITextFormatter
. For example, the file sink:
Log.Logger = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "./logs/myapp.json")
.CreateLogger();
XML <appSettings>
configuration
To specify the formatter in XML <appSettings>
provide its assembly-qualified type name:
<appSettings>
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:write-to:File.path" value="./logs/myapp.json" />
<add key="serilog:write-to:File.formatter"
value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
JSON appsettings.json
configuration
To specify formatter in json appsettings.json
provide its assembly-qualified type name:
{
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./logs/myapp.json",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
}
Rendered events
CompactJsonFormatter
will preserve the message template, properties, and formatting information so that the rendered message can be created at a later point. When the
JSON is intended for consumption in an environment without message template rendering, RenderedCompactJsonFormatter
can be used instead.
Instead of the message template, RenderedCompactJsonFormatter
writes the fully-rendered message, as well as
an event id generated from the message template, into the event:
{"@t":"2016-06-07T03:44:57.8532799Z","@m":"Hello, \"nblumhardt\"","@i":"7a8b9c0d","User":"nblumhardt"}
Format details
The format written by the compact formatters is specified generically so that implementations for other logging libraries, including Microsoft.Extensions.Logging, are possible if desired.
Payload
Each event is a JSON object with event data at the top level. Any JSON property on the payload object is assumed to be a regular property of the event, apart from the reified properties below.
Reified properties
The format defines a handful of reified properties that have special meaning:
Property | Name | Description | Required? |
---|---|---|---|
@t |
Timestamp | An ISO 8601 timestamp | Yes |
@m |
Message | A fully-rendered message describing the event | |
@mt |
Message Template | Alternative to Message; specifies a message template over the event's properties that provides for rendering into a textual description of the event | |
@l |
Level | An implementation-specific level identifier (string or number) | Absence implies "informational" |
@x |
Exception | A language-dependent error representation potentially including backtrace | |
@i |
Event id | An implementation specific event id (string or number) | |
@r |
Renderings | If @mt includes tokens with programming-language-specific formatting, an array of pre-rendered values for each such token |
May be omitted; if present, the count of renderings must match the count of formatted tokens exactly |
The @
sigil may be escaped at the start of a user property name by doubling, e.g. @@name
denotes a property called @name
.
Batch format
When events are batched into a single payload, a newline-delimited stream of JSON documents is required. Either \n
or \r\n
delimiters may be used. Batches of newline-separated compact JSON events can use the (unofficial) MIME type application/vnd.serilog.clef
.
Versioning
Versioning would be additive only, with no version identifier; implementations should treat any unrecognised reified properties as if they are user data.
Comparison
The output and benchmarks below compare the compact JSON formatter with Serilog's built-in JsonFormatter
.
Event
Log.Information("Hello, {@User}, {N:x8} at {Now}",
new
{
Name = "nblumhardt",
Tags = new[] { 1, 2, 3 }
},
123,
DateTime.Now);
Default JsonFormatter
292 bytes
{"Timestamp":"2016-06-07T13:44:57.8532799+10:00","Level":"Information","MessageT
emplate":"Hello, {@User}, {N:x8} at {Now}","Properties":{"User":{"Name":"nblumha
rdt","Tags":[1,2,3]},"N":123,"Now":"2016-06-07T13:44:57.8532799+10:00"},"Renderi
ngs":{"N":[{"Format":"x8","Rendering":"0000007b"}]}}
CompactJsonFormatter
187 bytes (0.64)
{"@t":"2016-06-07T03:44:57.8532799Z","@mt":"Hello, {@User}, {N:x8} at {Now}","@r
":["0000007b"],"User":{"Name":"nblumhardt","Tags":[1,2,3]},"N":123,"Now":2016-06
-07T13:44:57.8532799+10:00}
Formatting benchmark
See test/Serilog.Formatting.Compact.Tests/FormattingBenchmarks.cs
.
Formatter | Median | StdDev | Scaled |
---|---|---|---|
JsonFormatter |
11.2775 µs | 0.0682 µs | 1.00 |
CompactJsonFormatter |
6.0315 µs | 0.0429 µs | 0.53 |
JsonFormatter(renderMessage: true) |
13.7585 µs | 0.1194 µs | 1.22 |
RenderedCompactJsonFormatter |
7.0680 µs | 0.0605 µs | 0.63 |
Tools
Several tools are available for working with the CLEF format.
- Analogy.LogViewer.Serilog - CLEF parser for Analogy Log Viewer
clef-tool
- a CLI application for processing CLEF files- Compact Log Format Viewer - a cross-platform viewer for CLEF files
seqcli
- pretty-print
CLEF files at the command-line, oringest
CLEF files into Seq for search, and analysis- Serilog.Formatting.Compact.Reader - convert CLEF documents back into Serilog
LogEvent
s
Showing the top 20 packages that depend on Serilog.Formatting.Compact.
Packages | Downloads |
---|---|
Nuke.Build
Cross-platform build automation system
Signed by signpath.io from repository 'https://github.com/nuke-build/nuke' commit '46bbe7900f7f6426f377c713dd11d76e37516bc3' (see contained AppVeyorSettings.json file for build settings).
|
6 |
Nuke.Build
The AKEless Build System for C#/.NET
Signed by signpath.io from repository 'https://github.com/nuke-build/nuke' commit '011956b31c05f14f3233f6241cd6fbe038824d71' (see contained AppVeyorSettings.json file for build settings).
|
9 |
Nuke.Build
The AKEless Build System for C#/.NET
Signed by signpath.io from repository 'https://github.com/nuke-build/nuke' commit 'aeacff85068cb9218354491b39fbf916e5ea28e8' (see contained AppVeyorSettings.json file for build settings).
|
5 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
5 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
6 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
7 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
9 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
36 |
Serilog.AspNetCore
Serilog support for ASP.NET Core logging
|
124 |
Serilog.Extensions.Logging.File
Add file logging to ASP.NET Core apps with one line of code.
|
5 |
Version | Downloads | Last updated |
---|---|---|
3.0.0 | 0 | 06/09/2024 |
3.0.0-dev-00980 | 0 | 06/09/2024 |
2.0.1-dev-00969 | 5 | 05/28/2024 |
2.0.0 | 170 | 05/24/2024 |
2.0.0-dev-00962 | 5 | 05/28/2024 |
2.0.0-dev-00961 | 3 | 05/28/2024 |
1.1.1-dev-00944 | 5 | 04/28/2024 |
1.1.1-dev-00940 | 6 | 04/28/2024 |
1.1.1-dev-00939 | 4 | 04/29/2024 |
1.1.0 | 13,951 | 04/25/2022 |
1.1.0-dev-00934 | 2 | 05/28/2024 |
1.0.1-dev-00933 | 2 | 05/28/2024 |
1.0.1-dev-00929 | 4 | 04/28/2024 |
1.0.1-dev-00925 | 5 | 05/28/2024 |
1.0.1-dev-00922 | 2 | 05/31/2024 |
1.0.1-dev-00920 | 4 | 05/01/2024 |
1.0.0 | 6 | 10/28/2022 |
1.0.0-rc-916 | 4 | 04/28/2024 |
1.0.0-rc-915 | 5 | 05/19/2024 |
1.0.0-rc-912 | 3 | 05/28/2024 |
1.0.0-rc-9 | 5 | 04/28/2024 |
1.0.0-rc-6 | 7 | 05/26/2024 |
1.0.0-rc-5 | 4 | 05/01/2024 |
1.0.0-rc-3 | 4 | 05/26/2024 |
1.0.0-rc-10 | 6 | 05/26/2024 |