Serilog不会将日志写入AWS Elasticsearch Service

时间:2018-07-18 16:35:57

标签: amazon-web-services elasticsearch asp.net-core serilog amazon-elasticsearch

我正在使用Serilog在.NET Core应用程序中将日志写入AWS Elasticsearch Service,但是登录到Kibana时看不到任何日志。

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

我能够使用HttpClient成功获得回复。

此外,我还可以从浏览器加载Kibana和ElasticSearch网址。 请帮我解决我在这里缺少的东西。

编辑 在启动中连接时出现以下错误:

  

System.Net.Http.WinHttpException:无法建立与服务器的连接

1 个答案:

答案 0 :(得分:1)

我需要在连接设置中提供AWS Access Key和Secret Key才能使其按以下方式工作:

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

希望,将来对某人有帮助。