如何将 Serilog 与 Elastik Search Sink 结合使用并从应用程序设置中读取

问题描述 投票:0回答:1

我们目前正在使用serilog,我们的日志保存在本地。我们有一个代理将日志发送到 elastik。我们所有的配置都在应用程序设置中。

builder.Host.UseSerilog((ctx, configuration) => { configuration.ReadFrom.Configuration(ctx.Configuration); });

我们现在正在尝试将日志直接发送到 elastik。通过这种方法,我们可以将日志直接发送到 Elastik https://www.elastic.co/guide/en/ecs-logging/dotnet/current/serilog-data-shipper.html

.WriteTo.Elasticsearch(new [] { new Uri("http://localhost:9200" )},

但我们希望有一个类似于原始方法的解决方案,我们仍然可以写入 elastik 搜索,但它从配置中读取。请问有人对如何写入ElastikSearch有什么建议并且所有配置都在appsettings中吗?

c# elasticsearch
1个回答
0
投票

Elastic.Serilog.Sinks
appsettings.json
的示例。

appsettings.json
中的配置:

"Serilog": {
  "Using": [ "Serilog.Exceptions", "Elastic.Serilog.Sinks", "Serilog.Sinks.Console" ],
  "MinimumLevel": {
    "Default": "Warning"
  },
  "Enrich": [
    "FromLogContext",
    "WithEnvironmentUserName",
    "WithMachineName",
    "WithExceptionDetails"
  ],
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "{Message}{NewLine}{Exception}"
      }
    },
    {
      "Name": "Elasticsearch",
      "Args": {
        "bootstrapMethod": "Failure",
        "nodes": [ "http://elastic-cluster:9200/" ],
        "useSniffing": false,
        "dataStream": "api-logs",
        "includeHost": true,
        "includeUser": true,
        "includeProcess": true,
        "includeActivity": true,
        "debugMode": false
      }
    }
  ]
}

startup.cs
中读取配置并创建记录器。

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();
© www.soinside.com 2019 - 2024. All rights reserved.