Correlation logs are not reported in background service


Thanks to @GregKalapos I finally managed to listen to Http ougoing calls in an Asp .net core Background service and report them in APM.

But now I still have a point :
where can I found my correlated logs (with Serilog) in APM Kibana UI ?

Here is my appsettings.json

 "Serilog": {
    "Using": [],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Information",
        "Elastic": "Verbose"
"WriteTo": [
        "Name": "Console"
    "Enrich": [
    "Properties": {
      "ApplicationName": "myMicroservice"
  "ElasticApm": {
    "ServerUrls": "http://apm:8200"

My Serilog logger instance creator

            return new LoggerConfiguration()
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://elasticsearch:9200/"))
                AutoRegisterTemplate = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                        EmitEventFailureHandling.WriteToFailureSink |
                CustomFormatter = new EcsTextFormatter()

I'm using the nuget packages

Elastic.Apm.NetCoreAll v1.6.1
Elastic.Apm.SerilogEnricher v1.5.1
Elastic.CommonSchema.Serilog v1.5.1

My docker-compose.yml file

version: '3.4'


    image: ${DOCKER_REGISTRY-}myMicroservice
    container_name: myMicroservice
      context: .
      dockerfile: src/myMicroservice/src/Dockerfile
      - elasticsearch

    container_name: elasticsearch
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms4g -Xmx4g"
        soft: -1
        hard: -1
      - "9200:9200"
      - ./volumes/elasticsearch:/usr/share/elasticsearch/data

    container_name: apm
    command: >
       apm-server -e
         -E apm-server.rum.enabled=true
         -E setup.template.settings.index.number_of_replicas=0
         -E apm-server.kibana.enabled=true
         -E output.elasticsearch.hosts=["elasticsearch:9200"]
      - "8200:8200"
      - "8200"
      - elasticsearch
      - elasticsearch
      - kibana

    container_name: kibana
      - ELASTICSEARCH_URL=http://elasticsearch:9200
      - "5601:5601"
      - elasticsearch

My async Task called in a Background service ExecuteAsync method

        public async Task Test()
            await Agent.Tracer
            .CaptureTransaction("TransactionTest", ApiConstants.TypeRequest, async (t) =>
                _logger.LogInformation($"First Test log");

                await t.CaptureSpan("GetTestSpan1", "ElasticSearch", async span =>
                    _logger.LogInformation($"Test log 1");
                    _logger.LogWarning($"Test log 1");
                    _logger.LogError($"Test log 1");
                    _logger.LogCritical($"Test log 1");
                    await Task.FromResult(true);

                await t.CaptureSpan("GetTestSpan2", "ElasticSearch", async span =>
                    _logger.LogInformation($"Test log 2");
                    _logger.LogWarning($"Test log 2");
                    _logger.LogError($"Test log 2");
                    _logger.LogCritical($"Test log 2");
                    await Task.FromResult(true);

                await t.CaptureSpan("GetTestSpan3", "ElasticSearch", async span =>
                    _logger.LogInformation($"Test log 3");
                    _logger.LogWarning($"Test log 3");
                    _logger.LogError($"Test log 3");
                    _logger.LogCritical($"Test log 3");
                    await Task.FromResult(true);

And finally the output in APM

But where ever I click I'm not able to see any of my logs, where am I supposed to see them ? I guessed on clicking on the span within a transaction I could see them, could someone help on that ?

And finally the full Microservice + APM logs in verbose mode
APM missing correlated logs

Considering Serilog logger produces an index logstash-YYYY.MM.DD I managed to display correlated logs addind the following in Log indices of log Settings

Then using the Trace Logs from the Action link

I can see the logs

Is it the correct way to display logs from Serilog ?

Many thanks

@Nicolas_Rey sorry for the late reply - yes your solution is correct and that's the intended way this should work.

Basically what happens here is that with .Enrich.WithElasticApmCorrelationInfo() each time you log something, an enricher will tag the specific log line with the current trace id. The "Actions" -> "Trace logs" button on the APM UI will jump to the logs UI showing only the logs from the given trace for all services that participated in the trace. Similarly there is also a button on the logs UI which lets you jump to APM in case the log was tagged with a trace id.

Maybe one last note - but probably you already figured this out: this works only when there is an active transaction when the log line is created.

1 Like


Many thanks for your reply and yout high availability for the community, it's awesome!

Maybe an end to end tutorial for this kind of basics needs may be useful to other people, or maybe it already exists and I missed it :neutral_face: ?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.