How to Log to Elastic Search by NLog or SeriLog with authentications

Bashar Abu Shamaa picture Bashar Abu Shamaa · Apr 1, 2018 · Viewed 12.4k times · Source

We are currently use azure scale set (many VMs on one source group with load balance and one availability set), we used to use NLog to log our web app action and errors, but now we asked/needs to use Elastic Search and also use centralized log for all azure vm instances instead of file per each instance.

I am new to ES and LogStash concepts, Do I need to replace NLog with something else? and How I move to use ES and unify all logs in one (I think to make nlog store in azure storage table as unify results or do I needs to use LogStash or you prefer something else)?

What is the most logging that give support for .net core app on azure multi VMs as described above?

Any help please?

Answer

Julian picture Julian · Apr 3, 2018

For NLog there is a target "NLog.Targets.ElasticSearch" (nuget) which uses the Elasticsearch.Net package.

Usage:

<nlog>
  <extensions>
    <add assembly="NLog.Targets.ElasticSearch"/>
  </extensions>
  <targets>
    <target name="elastic" xsi:type="BufferingWrapper" flushTimeout="5000">
      <target xsi:type="ElasticSearch"  
         requireAuth="true"
         username="myUserName"
         password="coolpassword"/>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="elastic" />
  </rules>
</nlog>

Docs for the parameters: https://github.com/ReactiveMarkets/NLog.Targets.ElasticSearch/wiki

Please note:

  • If you need to use Elasticsearch.Net 6 (equivalent to Elastic Search version 6 or later), you need NLog.Targets.ElasticSearch version 5.
  • For Elasticsearch.Net 5 you need to use NLog.Targets.ElasticSearch 4