Serilog 不会将日志写入 AWS Elasticsearch Service

Serilog doesn't write logs into AWS Elasticsearch Service

我在我的 .NET Core 应用程序中使用 Serilog 将日志写入 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 url。 请帮助我解决我在这里遗漏的问题。

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

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

我需要在连接设置中提供 AWS 访问密钥和密钥才能使其正常工作,如下所示:

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

希望对以后的人有所帮助。