'Cannot authenticate using API Key with NLog.Targets.ElasticSearch

I cannot get NLog.Targets.ElasticSearch to connect to a local ElasticSearch instance using an APIKey.

My nlog.config has the following:

    <target xsi:type="BufferingWrapper" name="elasticSearch" flushTimeout ="5000">
      <target xsi:type="ElasticSearch" uri="https://dev.myinstance.com:9200" 
              includeAllProperties="true" 
              apiKey="MUhUUG1ZQUJFWE5GNDlPT3J5S3c123456783JRQS1CM1JtQVJzUWppdw=="
              apiKeyId="1HTPmYABEXNF49OOryKw"
              documentType=""
              index="test-${date:format=yyyy.MM.dd}"
              name="test">
      </target>
    </target>

The API Key has been verified using curl:

curl --insecure -H "Authorization: ApiKey MUhUUG1ZQUJFWE5GNDlPT3J5S3c123456783JRQS1CM1JtQVJzUWppdw==" https://dev.myinstance.com:9200/_security/api_key?pretty
{
  "api_keys" : [
    {
      "id" : "1HTPmYABEXNF49OOryKw",
      "name" : "test_import",
      "creation" : 1651847966642,
      "invalidated" : false,
      "username" : "elastic",
      "realm" : "reserved",
      "metadata" : { }
    }
  ]
}

Yet, nlog cannot auth:

Exception: Elasticsearch.Net.ElasticsearchClientException: Failed to ping the specified node. Call: Status code 401 from: HEAD
---> Elasticsearch.Net.PipelineException: Failed to ping the specified node. 
---> Elasticsearch.Net.PipelineException: Could not authenticate with the specified node. Try verifying your credentials or check your Shield configuration. 
---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.

I've tried adding requireAuth to the nlog.config with no luck.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source