Log Structured data to Elastic Search by using NLog 4.5 doesn't provide the ability to query in the fields

巧了我就是萌 提交于 2020-01-04 14:22:11

问题


Using NLog, when I log object/"structured data" to Elastic Search it stored in away that I can not make query on it, Please look to below image:

      <target xsi:type="ElasticSearch"
        name="MyElasticTarget"
        uri="url"
        requireAuth="true"
        username="MyUser"
        password="MyPass"
        Index="MyIndex-${date:format=yyyy.MM.dd}"
        >
        <layout xsi:type="JsonLayout" type='JsonLayout' IncludeAllProperties='true'>
          <attribute name='LogMessage' layout='${MySimpleClass:raw=true}' />
        </layout>
      </target>

  </targets>

And the code is:

  var _simpleObj = new MySimpleClass(5, "my structured Simple obj", "Sometype");
            var logger = LogManager.GetLogger("Example");
            logger.Info("{@MySimpleClass}", _simpleObj );

While When I log/store the same Structured data by using SeriLog, I can do query because I have the fields of object out of the message scope and as fields, please look to below image:

Any Idea how I can log structured data (objects) in be able to query them by use NLog?


回答1:


Have you tried to enable the includeAllProperties property for the NLog target:

<target xsi:type="ElasticSearch" includeAllProperties="true" />

You can also add your own custom fields:

<target xsi:type="ElasticSearch" includeAllProperties="true">
     <field name="messageTemplate" layout="${message:raw=true}" />
</target>



回答2:


I think you should use Logstash filters to decode you json string. I don't know if NLog supports Logstash filters but there are better and more powerful tools that satisfy your need. Log4Net is pretty powerful and widely used library and there is a nice Elasticsearch appneder library for it's Log4Stash which supports Logstash filters. It's efficient and easy to use. See link below

http://devthings.com.ua/logging-in-asp-net-web-api-using-log4net-and-elasticsearch/



来源:https://stackoverflow.com/questions/49720501/log-structured-data-to-elastic-search-by-using-nlog-4-5-doesnt-provide-the-abil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!