asp.net-mvc - Serilog 不会将日志写入 AWS Elasticsearch Service

标签 asp.net-mvc amazon-web-services elasticsearch serilog amazon-elasticsearch

我正在使用 Serilog 将日志写入 AWS Elasticsearch Service在我的 .NET Core 应用程序中,但在登录 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 访问 key 和 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();

希望,它对将来的人有所帮助。

关于asp.net-mvc - Serilog 不会将日志写入 AWS Elasticsearch Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51407025/

相关文章:

amazon-web-services - 适用于不同实例和相同端口的AWS负载均衡器

linux - Linux 上的 Maven "cannot find symbol"错误,但不是 Windows

javascript - 使用 logstash 和 elasticsearch

asp.net-mvc - MVC Controller 创建新记录

html - 如何解决 ASP.Net MVC 5 中的 glyphicons-halflings-regular.woff2 Err_Aborted 问题

amazon-web-services - 在 AWS 应用程序负载均衡器后面添加启用 x-pack 的 Elasticsearch 导致由于缺少身份验证而导致运行状况检查失败

ruby-on-rails - Searchkick分页,限制和偏移问题,滑轨

elasticsearch - 如何监控发送到 Elasticsearch 的查询

asp.net-mvc - 在 MVC 中绑定(bind)到 SelectList

json - ASP.Net MVC - Google Charts 的 Json 数据