19

I am far from being a Dev with any .net experience, but the dev team at work would like to use Serilog along with serilog-sinks-elasticsearch to push logs into my ELK stack.

Looking at the config for serilog-sinks-elasticsearch, there doesn't seem to be any way to send the creds require to write to the ElasticSearch Cluster.

Is this just a dumb ops person question or have I just missed the config somewhere?

Thanks

Paige Cook
  • 22,415
  • 3
  • 57
  • 68
Phil
  • 765
  • 2
  • 8
  • 26

4 Answers4

37

I struggled to find a good solution for this too. Adding the username/password to the url definitely does work but somehow doesnt feel right.

This worked for me:

.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("https://your-deployment.westeurope.azure.elastic-cloud.com:9243"))
{
  ...,
  ModifyConnectionSettings = x => x.BasicAuthentication("elastic", "your-password"),
})
Yashvit
  • 2,337
  • 3
  • 25
  • 32
10

Good question....

You might try supplying them as part of the Elasticsearch server/stack URL.

Example:

.WriteTo.Sink(new ElasticsearchSink(new ElasticsearchSinkOptions(new Uri(url))
{
    AutoRegisterTemplate = true
}

where

url = "https://user:password@stack-server:port"
programmerj
  • 1,634
  • 18
  • 29
  • It looks like specifying credentials in URL works for a single node only. If credentials are specified for several nodes divided by ; or , logging stops working. Tried in .Net Core 3.1. Settings in appsettings.json – MikhailSP Jan 18 '21 at 12:29
8

If you want to stick with your configuration file (appsettings.json), knowing that you can use the argument connectionGlobalHeaders to specify the login/password, because Elasticsearch use Basic Authentication to authenticate the user.

Something like this:

"Serilog": {
    "WriteTo": [
      {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "https://your-uri",
          "connectionGlobalHeaders": "Authorization=Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
          "indexFormat": "application-log-{0:yyyy.MM}",
          "autoRegisterTemplate": true,
          "autoRegisterTemplateVersion": "ESv7"
        }
      }
    ]
  }

where the part after "Authorization=Basic" is the Base64 string of your "login:password".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marimout
  • 205
  • 4
  • 8
0

If Serilog doesn't sends data to your log server make sure that the version of elastic is compatible with the sink, It took me hours to figure that out :)

mahdignb
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '22 at 12:38