bcuff / elasticsearch-net-aws

Add-on to Elasticsearch.Net & NEST for using AWS's elasticsearch service.
Apache License 2.0
72 stars 27 forks source link

Suggestion #58

Closed zewar96 closed 4 years ago

zewar96 commented 4 years ago

I wanted to leave a suggestion because i wasted FAR too many hours trying to find a way to do it and it was right in front of me. You recommend the following for Serilog

 const string esUrl = "https://aws-es-thinger.us-west-1.es.amazonaws.com";
  Log.Logger = new LoggerConfiguration()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esUrl))
                {
                    ModifyConnectionSettings = conn =>
                    {
                        var httpConnection = new AwsHttpConnection("us-east-1");
                        var pool = new SingleNodeConnectionPool(new Uri(esUrl));
                        return new ConnectionConfiguration(pool, httpConnection);
                    }
                })
                .CreateLogger();

This works and is fine, but doesn't allow for Configuration-based setup to be used. I would recommend the following which would also allow for people who use appSettings.json as their method for loading

 const string esUrl = "https://aws-es-thinger.us-west-1.es.amazonaws.com";
  Log.Logger = new LoggerConfiguration()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esUrl))
                {
                    Connection = new AwsHttpConnection()
                })
                .CreateLogger();

In the Serilog configuration you can now do this:

{
    "Serilog": {
        "Using": ["Serilog", "Serilog.Exceptions", "Serilog.Sinks.Elasticsearch", "Serilog.Enrichers.Environment", "Serilog.Enrichers.Process"],
        "MinimumLevel": "Debug",
        "WriteTo": [{
                "Name": "Elasticsearch",
                "Args": {
                    "nodeUris": "https://******.us-east-1.es.amazonaws.com",
                    "numberOfShards": 5,
                    "numberOfReplicas": 10,
                    "connection": "Elasticsearch.Net.Aws.AwsHttpConnection, Elasticsearch.Net.Aws"
                }
            }
        ],
        "Enrich": ["FromLogContext", "WithMachineName", "WithExceptionDetails"],
    }
}
bcuff commented 4 years ago

Thank you for our suggestion. I added your snippet to the README here