【问题标题】:Store an integer value into ElasticSearch when using Log4Net使用 Log4Net 时将整数值存储到 ElasticSearch
【发布时间】:2016-07-27 10:02:31
【问题描述】:

我在 ElasticSearch 中记录用户操作并且我正在使用 C# Log4Net

我正在使用 C# NEST 库来访问 ElasticSearch 数据库。

我的日志行如下所示:

{
      "_index" : "log-2016.07.27",
      "_type" : "logEvent",
      "_id" : "AVYrwmW5Hc5CAgECpn_X",
      "_score" : 1.0,
      "_source" : {
        "timeStamp" : "2016-07-27T09:49:35.3774113Z",
        "message" : "Upload file operation took 11683 ms",
        "loggerName" : "Reviewer.Web.WebApi.GroupsController",
        "identity" : "",
        "level" : "INFO",
        "properties" : {
          "log4net:UserName" : "CORP\\g",
          "log4net:ElapsedTime" : "11683",
          "log4net:Identity" : "",
          "IP" : "::1",
          "log4net:HostName" : "GBWOTIOM68052D",
          "@timestamp" : "2016-07-27T09:49:35.3774113Z"
        }
      } 

我希望将 log4net:ElapsedTime 值存储为整数而不是字符串。

目前我在存储经过的时间时这样做:

long ms = 1000;
LogicalThreadContext.Properties["log4net:ElapsedTime"] = ms;

我知道我应该指定一个模板来告诉 ElasticSearch 将经过的值存储为一个整数,但是该怎么做呢?

【问题讨论】:

    标签: c# elasticsearch asp.net-web-api log4net elasticsearch-2.0


    【解决方案1】:

    如果您希望 elasticsearch 识别您的字段,您应该发送不带双引号的数据值。

    curl -XPUT 'localhost:9200/tmp/tmp/1' -d '{
       "field1":"3",
       "field2":3
    }'
    

    -

    curl -XGET 'localhost:9200/tmp'
    {"tmp":{"aliases":{},"mappings":{"tmp":{"properties":{"field1":{"type":"string"},"field2":{"type":"long"}}}},"settings":{"index":{"creation_date":"1469621916488","uuid":"Qj64-CU5RUW6ShOyRqLZXQ","number_of_replicas":"0","number_of_shards":"1","version":{"created":"1070599"}}},"warmers":{}}}
    

    您可以看到 field1 是字符串,但 field2 是数字。

    【讨论】:

    猜你喜欢
    • 2012-04-29
    • 2018-12-13
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多