markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

Log not written in Elasticsearch with connectionStringName #65

Closed jpges closed 6 years ago

jpges commented 6 years ago

Dear all, I'm trying to get working the NLog with target to Elasticsearch in my asp.net web API application. I'm working with .net Core 2. I'm using two docker containers, one for my api and one for a ElasticSearch + Kibana.

My docker-compose.yml looks like:

services: msa.logapisample: image: msa.logapisample:${TAG:-latest} container_name: msa.servicio.api build: context: . dockerfile: ./MSA.LogAPISample/Dockerfile depends_on:

  • elasticsearch-kibana

    elasticsearch-kibana: image: nshou/elasticsearch-kibana container_name: msa.elasticsearch-kibana

I use NLog.Targets.ElasticSearch V 5.0.0 and NLog.Web.AspNerCore 4.5.2

If I use NLog.config below:

` <target xsi:type="ElasticSearch" connectionStringName="ElasticsearchUrl" index="tcpymes-${date:format=yyyy.MM.dd}" includeAllProperties="true" requireAuth="true" layout="NLOG.DESARROLLO: ${message}" userName="user" password="pwd" documentType="application-nlog">

    <field name="EventLevel" layout="${level}" />
    <field name="Host" layout="${machinename}" />
    <field name="Logger" layout="${logger}" />
    <field name="EventMessage" layout="${message}" />
    <field name="BaseDirectory" layout="${basedir}" />
    <field name="SiteName" layout="${iis-site-name}"/>
  </target>
</target>`

With this appsettings.json:

{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "None" } }, "ElasticsearchUrl": "http://elasticsearch-kibana:9200" }

Then target "elastic" doesn't work.

But if I use the Nlog.config below:

` <target xsi:type="ElasticSearch" uri="http://elasticsearch-kibana:9200" index="tcpymes-${date:format=yyyy.MM.dd}" includeAllProperties="true" requireAuth="true" layout="NLOG.DESARROLLO: ${message}" userName="user" password="pwd" documentType="application-nlog">

    <field name="EventLevel" layout="${level}" />
    <field name="Host" layout="${machinename}" />
    <field name="Logger" layout="${logger}" />
    <field name="EventMessage" layout="${message}" />
    <field name="BaseDirectory" layout="${basedir}" />
    <field name="SiteName" layout="${iis-site-name}"/>
  </target>
</target>`

Then target works allright.

Some idea?

aheubusch commented 6 years ago

This should work (appsettings.json):

{
  "ConnectionStrings": {
    "ElasticsearchUrl": "..."
  },
  "Logging": { }
}
jpges commented 6 years ago

It works fine. Thank you

another-mi commented 5 years ago

It not works for me. Asp Net Core 3.0 (NLog.Targets.ElasticSearch 7.1.0) Error: "ElasticSearch: Failed to send log messages. status= Exception: Elasticsearch.Net.ElasticsearchClientException: Failed to ping the specified node.. Call: Status code unknown from: HEAD /"

snakefoot commented 5 years ago

@another-mi Please create new issue instead of disturbing old issues.

Please also include Nlog InternalLogger-output and your NLog.config in the new github-issue.

It is also a good idea to read the documentation (Notice NetCore-users should use ${configsetting}):

https://github.com/markmcdowell/NLog.Targets.ElasticSearch/wiki#example-of-appsettingsjson