We have a microservice architecture running in a kubernetes cluster, and are using the Elastic stack (deployed using EKS providers) for monitoring everything. One of the last aspects I'm struggling with, is how to correctly log to to elastic search, from an ASP.NET Core 5.x microservice.
If I understand correctly, I can log directly to Elastic Search using a combination of Serilog, Elastic search sink for serilog, and the elastic common schema (ESC) package.
I've setup Serilog according to the docs, like so:
Log.Logger = new LoggerConfiguration()
.Enrich.WithElasticApmCorrelationInfo()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
{
CustomFormatter = new EcsTextFormatter()
})
.CreateLogger();
This results in logs being sent to a logstash-{yyyy}-{mm}-{dd}
index.
This however will not show those logs in the default 'Observability > Logs' view in Kibana, since the index pattern for that screen is set to logs-*,filebeat-*,kibana_sample_data_logs*
by default.
So I thought I would need to send the logs to a logs-*
index, but found out those are 'data streams' instead of plain old indices. Reading up on data streams, I thought it would be better to send logs to a data stream vs a plain old index, because it should offer better performance (automatic index splitting, vs having 1 index per service, per calendar day), so I modified my serilog elastic sink configuration as follows:
Log.Logger = new LoggerConfiguration()
.Enrich.WithElasticApmCorrelationInfo()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
{
AutoRegisterTemplate = true,
IndexFormat = $"logs-my-micro-service",
BatchAction = ElasticOpType.Create,
CustomFormatter = new EcsTextFormatter()
})
.CreateLogger();
This seems to work fine at first glance, logs are correctly arriving in the data stream index, and are visible with the default 'Logs' screen configuration.
However, with unhandled exception logs, the logs sent to the data stream index are missing the error.stack_trace
(string
type) property.
I'm at a loss as to where to find the source of this problem, I'm also not sure whether the configuration I want to use (using data streams) is the recommended option.