2

I want to log to elasticsearch using serilog.

I have added Elastic.Serilog.Sinks package to my application. But there is no way to pass username and password while configuring serilog elasticsearch config.

My assumption is that I would need to pass log index name, url, username, password while configuring serilog with elasticsearch sink.

https://www.elastic.co/guide/en/ecs-logging/dotnet/current/serilog-data-shipper.html

Above url has example to configure using url. Also, there is no index name.

Is log index name and data stream name same ?

How to pass username and password while configuring serilog elasticsearch configuration ?

2
  • Have you gone through this ? nuget.org/packages/… Commented Jul 29, 2024 at 18:30
  • Yes, but it doesn't mention how to pass username and password while configuring. Commented Jul 30, 2024 at 6:13

1 Answer 1

4

You could install Elastic.Clients.Elasticsearch and try this

var settings = new ElasticsearchClientSettings(new Uri("https://10.96.5.5:9200"))
        .CertificateFingerprint("xxx")
        .Authentication(new BasicAuthentication("elastic", "xxx"));

var esClient = new ElasticsearchClient(settings);

var esOptions = new ElasticsearchSinkOptions(esClient.Transport)
{
    BootstrapMethod = BootstrapMethod.Failure,
};

Log.Logger = new LoggerConfiguration()
    .WriteTo.Elasticsearch(esOptions)
    .CreateLogger();

Or this:

Log.Logger = new LoggerConfiguration()
 .WriteTo.Elasticsearch([new Uri("https://10.96.5.5:9200")], opts =>
 {
     opts.BootstrapMethod = BootstrapMethod.Failure;
 }, transport =>
 {
     transport.CertificateFingerprint("xxx");
     transport.Authentication(new BasicAuthentication("elastic", "xxx"));
 })
 .CreateLogger();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.