Serilog ElasticSearch Sink 自定义属性

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

我正在尝试将安装了

Serilog.AspNetCore
Serilog.Sinks.Elasticsearch.NET 6.0 Web Api 的一些数据记录到我的 Elasticsearch 实例中,但我需要使用自定义属性,例如:

  • 银行名称
  • 总价值
  • 客户姓名

和其他类似的,但我创建了自定义

Enricher

    public class ApiEnricher : ILogEventEnricher
    {
        private const string bankName = "Total Bank";
        private const decimal totalValue = 21;
        private const string customerName = "Adam";

        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            // Add default values for required fields
            logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("bankName", bankName));
            logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("total.value", totalValue));
            logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("cusotmer.name", cusomerName));
        }
    }

我尝试将其添加到我的 appsettings.json 中:

    "Serilog": {
        "Using": [ "Serilog.Sinks.Elasticsearch", "WebApi.Common" ],
        "MinimumLevel": "Debug",
        "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "ApiEnricher" ],
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}"
                }
            },
            {
                "Name": "Elasticsearch",
                "Args": {
                    "nodeUris": "http://host.docker.internal:9200",
                    "indexFormat": "logs-myApp-example-{0:yyyy.MM}",
                    "autoRegisterTemplate": true,
                    "inlineFields": true,
                    "numberOfReplicas": 2,
                    "numberOfShards": 2
                }
            }
        ]
    },

但是运气不好,我得到的唯一示例输出是:

{
  "@timestamp": "2023-11-27T14:02:24.2061221+00:00",
  "level": "Information",
  "messageTemplate": "Example {bankName} {totalValue} {customerName}",
  "message": "Example \"Forbank\" 21.0 \"Bar\"",
  "bankName": "Forbank",
  "totalValue": 21.0,
  "customerName": "Bar",
  "SourceContext": "WebApi.Controllers.SolutionController",
  "ActionId": "1a52a98f-ecc6-40f2-a632-dc045d53811b",
  "ActionName": "WebApi.Controllers.SolutionController.Init",
  "RequestId": "0HMVFA5SJN8GJ:00000004",
  "RequestPath": "/api/example/init",
  "ConnectionId": "0HMVFA5SJN8GJ"
}

记录方式:logger.LogInformation("示例 {bankName} {totalValue} {customerName}",bankName, 21, "Bar");

有效,但我想要不强制重写每个日志的解决方案,如果可能的话,我希望它可以从

appsettings.json
进行配置,并且我更喜欢这些道具仅在Elasticsearch接收器上可用。

编辑:

让我澄清一下问题从何而来。

我收到了一家外部公司关于发送到 Elastic 的日志应是什么样子的要求。

这些需求中,我有一个必须出现在elastic中的参数列表,例如:

user.name
user.phone
class
exception

我想知道是否可以通过某种巧妙的方式来完成,而不必重做所有日志。

c# serilog serilog-aspnetcore serilog-sinks-elasticsearch
1个回答
0
投票

这可以通过

appsettings.json
配置来完成,添加
Properties
部分,请参阅 官方文档中的示例:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Elasticsearch" ],
    "MinimumLevel": "Warning",
    "WriteTo": [
      {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "http://localhost:9200"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Properties": {
      "Application": "My app"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.