Elastic.Serilog.Sinks
ASerilog sink that writes logs directly toElasticsearch orElastic Cloud
Add a reference to theElastic.Serilog.Sinks package:
<PackageReference Include="Elastic.Serilog.Sinks" Version="8.6.0" />There’s a few ways that you can extend aSerilogLoggerConfiguration:
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().Enrich.FromLogContext()NOTE: Don’t forget we also publish anElastic.Apm.SerilogEnricher for the Elastic APM Agent!
Writing toElasticsearch
.WriteTo.Elasticsearch(new [] { new Uri("http://localhost:9200" )}, opts =>{opts.DataStream = new DataStreamName("logs", "console-example", "demo");opts.BootstrapMethod = BootstrapMethod.Failure;opts.ConfigureChannel = channelOpts =>{channelOpts.BufferOptions = new BufferOptions{ConcurrentConsumers = 10};};}, transport =>{// transport.Authentication(new BasicAuthentication(username, password));// transport.Authentication(new ApiKey(base64EncodedApiKey));})- Basic Auth
- ApiKey
Writing toElastic Cloud:
.WriteTo.ElasticCloud("cloudId", "cloudUser", "cloudPass", opts =>opts is an instance ofElasticsearchSinkOptions with the following options
| Option | Description |
|---|---|
Transport | An instance ofElastic.Transport that dictates where and how we are communicating to. Defaults tohttp://localhost:9200 |
DataStream | Where to write data, defaults to thelogs-dotnet-default datastream. |
BootstrapMethod | Wheter the sink should attempt to install component and index templates to ensure the datastream has ECS mappings. Can be be eitherNone (the default),Silent (attempt but fail silently),Failure (attempt and fail with exceptions if bootstrapping fails). |
TextFormatting | Allows explicit control of over theEcsTextFormatterConfiguration used to emit ECS json documents. SeeElastic.CommonSchema.Serilog for available options. |
ConfigureChannel | A callback receiving theDatastreamChannelOptions which allows you to control sizing, backpressure etc. SeeElastic.Ingest.Elasticsearch for more information. |
Note that you can also passElasticsearchSinkOptions directly
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(client.Transport))This allows you to reuse theTransport used by the Elasticsearch Client for instance.
When Elasticsearch security features are enabled, requests without a valid authentication header will be rejected. You can enable authentication via one of the methods below:
Basic Auth
.WriteTo.Elasticsearch(new [] { new Uri("http://localhost:9200" )}, opts =>{...}, transport =>{transport.Authentication(new BasicAuthentication(username, password));})- Basic authentication
API Key
.WriteTo.Elasticsearch(new [] { new Uri("http://localhost:9200" )}, opts =>{...}, transport =>{transport.Authentication(new ApiKey(base64EncodedApiKey));})- API Key
To learn more about authentication with the Elastic Stack, seeUser Authentication.
This sink by proxy of its formatter allows you to set ECS fields directly from the message template using properties that adhere to thehttps://messagetemplates.org/ format.
The available ECS message template properties are listed underLogTemplateProperties.* e.gLogTemplateProperties.TraceId
Log.Information("The time is {TraceId}", "my-trace-id");Will overridetrace.id on the resulting ECS json document.
In case of issues, you can enable theSerilog Self-Log feature to expose any error you might have encountered.
Serilog.Sinks.Elasticsearchis an amazing community led sink that has a ton of options and works against older Elasticsearch versions< 8.0.Serilog.Sinks.Elasticsearchis unofficially supported by Elastic with some of the .NET team helping to maintain it.Elastic.Serilog.Sinksisofficially supported by Elastic and was purposely build to adhere to newer best practices around logging, datastreams and ILM.Elastic.Serilog.Sinksis purposely build to have fewer configuration options and be more prescriptive thanSerilog.Sinks.Elasticsearch.- That is not to say there aren’t plenty of configuration hooks in
Elastic.Serilog.Sinks
Elastic.Serilog.Sinksonly works withElasticsearch 8.xand up.- This is because the bootrapping (
BootstrapMethod) attempts to load templates build for Elasticsearch 8.0 and up. Elastic.Serilog.Sinkshas only one way it emits data to Elasticsearch confirming to theecs-logging specification- That doesn’t mean you can not introduce your own additional properties though.
Elastic.Serilog.Sinkshas no durable mode.- If you need higher guarantees on log delivery use
Serilog.Sinks.Filewith ourECS log formatter for Serilog and usefilebeat to ship these logs. - Check outElastic Agent and Fleet to simplify collecting logs and metrics on the edge.
If you miss a particular feature fromSerilog.Sinks.Elasticsearch inElastic.Serilog.Sinks please open afeature request! We’d love to grow this sink organically moving forward.