# Correlation logs are not reported in background service

**URL:** <https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565>\
**Category:** APM\
**Tags:** docker, dotnet\
**Created:** [October 19, 2020, 2:43pm UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565 "2020-10-19T14:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Rey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_rey/32/47284_2.png) [@Nicolas\_Rey](https://discuss.elastic.co/u/Nicolas_Rey)\
**Post date:** [October 19, 2020, 2:43pm UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565/1 "2020-10-19T14:43:23Z")

</div>

Hi,

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**

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

```

My Serilog logger instance creator

```auto
            return new LoggerConfiguration()
            .ReadFrom.Configuration(config)
            .Enrich.WithElasticApmCorrelationInfo()
            .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 |
                                        EmitEventFailureHandling.RaiseCallback,
                CustomFormatter = new EcsTextFormatter()
            })
            .WriteTo.OpenTracing()
            .CreateLogger();

```

I'm using the nuget packages

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

```

My docker-compose.yml file

```auto
version: '3.4'

services:

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

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
    container_name: elasticsearch
    environment:
      - discovery.type=single-node
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms4g -Xmx4g"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - "9200:9200"
    volumes:
      - ./volumes/elasticsearch:/usr/share/elasticsearch/data

  apm:
    image: docker.elastic.co/apm/apm-server:7.9.2
    container_name: apm
    command: >
       apm-server -e
         -E apm-server.rum.enabled=true
         -E setup.kibana.host=kibana:5601
         -E setup.template.settings.index.number_of_replicas=0
         -E apm-server.kibana.enabled=true
         -E apm-server.kibana.host=kibana:5601
         -E output.elasticsearch.hosts=["elasticsearch:9200"]
    ports:
      - "8200:8200"
    expose:
      - "8200"
    links:
      - elasticsearch
    depends_on:
      - elasticsearch
      - kibana

  kibana:
    image: docker.elastic.co/kibana/kibana:7.9.2
    container_name: kibana
    environment:
      - ELASTICSEARCH_URL=http://elasticsearch:9200
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch

```

My async Task called in a Background service ExecuteAsync method

```auto
        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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/d/3d7f7fb242b8834985b5dd2560f259c11af6a32c.png)

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](https://gofile.io/d/VAlWj0)

---

<div class="post-metadata">

**Author:** ![Nicolas\_Rey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_rey/32/47284_2.png) [@Nicolas\_Rey](https://discuss.elastic.co/u/Nicolas_Rey)\
**Post date:** [October 19, 2020, 4:54pm UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565/2 "2020-10-19T16:54:14Z")

</div>

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**

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/6/e65ead61c8aea4bda451e71de9a92543debf7562.png)

Then using the **Trace Logs** from the **Action** link

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/4/b4678564bcfe182636fe7354321f4a8beacc5d12.png)

I can see the logs

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/6/c6151852d2fbc45fa4b88279a3ff97d1f685e46e.png)

**Is it the correct way to display logs from Serilog ?**

Many thanks

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [October 22, 2020, 1:20pm UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565/3 "2020-10-22T13:20:27Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Rey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_rey/32/47284_2.png) [@Nicolas\_Rey](https://discuss.elastic.co/u/Nicolas_Rey)\
**Post date:** [October 22, 2020, 1:38pm UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565/4 "2020-10-22T13:38:10Z")

</div>

@GregKalapos

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 😐 ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 12, 2020, 9:38am UTC](https://discuss.elastic.co/t/correlation-logs-are-not-reported-in-background-service/252565/5 "2020-11-12T09:38:16Z")

</div>

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