Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Compact JSON event format for Serilog

License

NotificationsYou must be signed in to change notification settings

serilog/serilog-formatting-compact

Repository files navigation

A simple, compact JSON-based event format for Serilog.CompactJsonFormatter significantly reduces the byte count of small log events when compared with Serilog's defaultJsonFormatter, while remaining human-readable. It achieves this through shorter built-in property names, a leaner format, and by excluding redundant information.

Sample

A simpleHello, {User} event.

{"@t":"2016-06-07T03:44:57.8532799Z","@mt":"Hello, {User}","User":"nblumhardt"}

Getting started

Install fromNuGet:

dotnet add package Serilog.Formatting.Compact

The formatter is used in conjunction with sinks that acceptITextFormatter. For example, thefile sink:

Log.Logger=newLoggerConfiguration().WriteTo.File(newCompactJsonFormatter(),"./logs/myapp.json").CreateLogger();

XML<appSettings> configuration

To specify the formatter in XML<appSettings> provide its assembly-qualified type name:

<appSettings>  <addkey="serilog:using:File"value="Serilog.Sinks.File" />  <addkey="serilog:write-to:File.path"value="./logs/myapp.json" />  <addkey="serilog:write-to:File.formatter"value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />

JSONappsettings.json configuration

To specify formatter in jsonappsettings.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 theJSON 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 asanevent idgenerated 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, includingMicrosoft.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:

PropertyNameDescriptionRequired?
@tTimestampAn ISO 8601 timestampYes
@mMessageA fully-rendered message describing the event
@mtMessage TemplateAlternative to Message; specifies amessage template over the event's properties that provides for rendering into a textual description of the event
@lLevelAn implementation-specific level identifier (string or number)Absence implies "informational"
@xExceptionA language-dependent error representation potentially including backtrace
@iEvent idAn implementation specific event id (string or number)
@rRenderingsIf@mt includes tokens with programming-language-specific formatting, an array of pre-rendered values for each such tokenMay be omitted; if present, the count of renderings must match the count of formatted tokens exactly
@trTrace idThe id of the trace that was active when the event was created, if any
@spSpan idThe id of the span that was active when the event was created, if any

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 typeapplication/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-inJsonFormatter.

Event

Log.Information("Hello, {@User}, {N:x8} at {Now}",new{Name="nblumhardt",Tags=new[]{1,2,3}},123,DateTime.Now);

DefaultJsonFormatter 292 bytes

{"Timestamp":"2016-06-07T13:44:57.8532799+10:00","Level":"Information","MessageTemplate":"Hello, {@User}, {N:x8} at {Now}","Properties":{"User":{"Name":"nblumhardt","Tags":[1,2,3]},"N":123,"Now":"2016-06-07T13:44:57.8532799+10:00"},"Renderings":{"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

Seetest/Serilog.Formatting.Compact.Tests/FormattingBenchmarks.cs.

FormatterMedianStdDevScaled
JsonFormatter11.2775 µs0.0682 µs1.00
CompactJsonFormatter6.0315 µs0.0429 µs0.53
JsonFormatter(renderMessage: true)13.7585 µs0.1194 µs1.22
RenderedCompactJsonFormatter7.0680 µs0.0605 µs0.63

Tools

Several tools are available for working with the CLEF format.

Customizing output

Serilog.Formatting.Compact is not intended to provide customizable formatters. Seethis blog post for comprehensive Serilog JSON output customization examples.

About

Compact JSON event format for Serilog

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp