Elastic.CommonSchema.Serilog 8.6.1
Elastic Common Schema Serilog Text Formatter
This ITextFormatter
implementation formats a Serilog event into a JSON representation that adheres to the Elastic Common Schema specification.
Packages
The .NET assemblies are published to NuGet under the package name Elastic.CommonSchema.Serilog
How to Enable
var logger = new LoggerConfiguration()
.WriteTo.Console(new EcsTextFormatter())
.CreateLogger();
In the code snippet above new EcsTextFormatter()
enables the text formatter and instructs Serilog to format the event as JSON. The sample above uses the Console sink, but you are free to use any sink of your choice, perhaps consider using a filesystem sink and Elastic Filebeat for durable and reliable ingestion.
In ASP.NET (core) applications
.UseSerilog((ctx, config) =>
{
// Ensure HttpContextAccessor is accessible
var httpAccessor = ctx.Configuration.Get<HttpContextAccessor>();
config
.ReadFrom.Configuration(ctx.Configuration)
.Enrich.WithEcsHttpContext(httpAccessor)
.WriteTo.Async(a => a.Console(new EcsTextFormatter()));
})
The WithEcsHttpContext
ensures logs will be enriched with HttpContext
data.
An example of the output is given below:
{
"@timestamp": "2019-11-22T14:59:02.5903135+11:00",
"log.level": "Information",
"message": "Log message",
"ecs": {
"version": "1.4.0"
},
"event": {
"severity": 0,
"timezone": "AUS Eastern Standard Time",
"created": "2019-11-22T14:59:02.5903135+11:00"
},
"log": {
"logger": "Elastic.CommonSchema.Serilog"
},
"process": {
"thread": {
"id": 1
},
"executable": "System.Threading.ExecutionContext"
}
}
Configuration
Option | Description | |
---|---|---|
MapCurrentThead |
true map ecs.process by looking up the Process from the current thread |
|
MapHttpAdapter |
null a way to map HttpContextAccessor to ECS fields. |
|
LogEventsPropertiesToFilter |
A Set<string> of properties that should not be emitted as labels.* or metadata.* |
|
MapCustom |
A Func that allows you to mutate the EcsDocument before its fully converted. |
ECS Aware Message Templates
This formatter also allows you to set ECS fields directly from the message template using properties that adhere to the https://messagetemplates.org/ format.
The available ECS message template properties are listed under LogTemplateProperties.*
e.g LogTemplateProperties.TraceId
Log.Information("The time is {TraceId}", "my-trace-id");
Will override trace.id
on the resulting ECS json document.
Copyright and License
This software is Copyright (c) 2014-2020 by Elasticsearch BV.
This is free software, licensed under: The Apache License Version 2.0.
No packages depend on Elastic.CommonSchema.Serilog.
.NET Framework 4.6.1
- Elastic.CommonSchema (>= 8.6.1)
- Serilog (>= 2.9.0)
.NET Standard 2.0
- Elastic.CommonSchema (>= 8.6.1)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.1.0)
- Microsoft.AspNetCore.Http.Extensions (>= 2.1.0)
- Serilog (>= 2.9.0)
.NET Standard 2.1
- Elastic.CommonSchema (>= 8.6.1)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.1.0)
- Microsoft.AspNetCore.Http.Extensions (>= 2.1.0)
- Serilog (>= 2.9.0)
Version | Downloads | Last updated |
---|---|---|
8.12.3 | 14 | 12/13/2024 |
8.12.2 | 17 | 10/26/2024 |
8.12.1 | 14 | 10/26/2024 |
8.12.0 | 16 | 10/26/2024 |
8.11.1 | 30 | 06/11/2024 |
8.11.0 | 18 | 06/08/2024 |
8.6.1 | 13 | 01/08/2024 |
8.6.0 | 17 | 06/13/2023 |
8.4.0-alpha4 | 18 | 06/13/2023 |
8.4.0-alpha3 | 17 | 06/13/2023 |
8.4.0-alpha2 | 18 | 06/12/2023 |
8.4.0-alpha1 | 18 | 06/12/2023 |
1.6.0-alpha1 | 15 | 06/13/2023 |
1.5.3 | 26 | 06/28/2022 |
1.5.1 | 15 | 06/13/2023 |
1.5.0 | 14 | 06/14/2023 |
1.4.4 | 13 | 06/13/2023 |
1.4.3 | 13 | 06/13/2023 |
1.4.2 | 16 | 06/13/2023 |
1.4.1 | 14 | 02/16/2024 |
1.4.0 | 14 | 06/13/2023 |
1.4.0-beta1 | 14 | 02/16/2024 |