问题
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