asp.net-coreelasticsearchloggingserilogserilog-sinks-elasticsearch

How to log using serilog to elasticsearch with Elastic.Serilog.Sinks in .net application


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 ?


Solution

  • 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();