2

I am using Serilog to write logs into AWS Elasticsearch Service in my .NET Core application but when logging into Kibana I don't see any logs written.

public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
    const string esUrl = "https://aws-es-thinger.us-west-1.es.amazonaws.com";
    Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .Enrich.WithExceptionDetails()
        .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esUrl))
        {
            ModifyConnectionSettings = conn =>
            {
                var httpConnection = new AwsHttpConnection("us-east-1");
                var pool = new SingleNodeConnectionPool(new Uri(esUrl));
                var conf = new ConnectionConfiguration(pool, httpConnection);
                return conf;
            },
            AutoRegisterTemplate = true
        }).CreateLogger();
}

I am able to use HttpClient to get response successfully.

Also, I am able to load the Kibana and ElasticSearch urls from my browser. Please help me with what am I missing here.

EDIT Getting below error when connecting in Startup:

System.Net.Http.WinHttpException: A connection with the server could not be established

6
  • 1
    Did you try enabling self log to the console to see if there was some sort of logging error? Commented Jul 18, 2018 at 17:13
  • @mason Thanks using it helped me to get the error. Am getting exception "UnexpectedElasticsearchClientException: A connection with the server could not be established" Commented Jul 18, 2018 at 17:32
  • Sounds like a wrong URL, or perhaps a firewall issue. Commented Jul 18, 2018 at 17:35
  • Hmm, I am able to access the same url from chrome browser Commented Jul 18, 2018 at 17:38
  • Did you ever figure out what the problem was? I'm having the same issue right now. Commented Aug 17, 2018 at 21:41

1 Answer 1

7

I needed to provide AWS Access Key and Secret Key in Connection Settings to get it work as below:

Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
  .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(awsSettings.ElasticSearchUrl))
  {
      ModifyConnectionSettings = conn =>
      {
          var httpConnection = new AwsHttpConnection(awsSettings.Region
              , new StaticCredentialsProvider(new AwsCredentials
              {
                  AccessKey = awsSettings.AccessKey,
                  SecretKey = awsSettings.SecretKey,
              }));
          var pool = new SingleNodeConnectionPool(new Uri(awsSettings.ElasticSearchUrl));
          var conf = new ConnectionConfiguration(pool, httpConnection);
          return conf;
      },
      ConnectionTimeout = new TimeSpan(0, 10, 0),
      IndexFormat = "xxxxx-{0:yyyy.MM}",
      FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
      EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
           EmitEventFailureHandling.WriteToFailureSink |
           EmitEventFailureHandling.RaiseCallback,
      FailureSink = new LoggerConfiguration().WriteTo
      .RollingFile(new JsonFormatter(), "XXXXX-{Date}.txt").CreateLogger()
  })
.CreateLogger();

Hope, it helps somebody in future.

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.