markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

StackOverflowException raised when logging object which has self-referencing loop field #99

Closed lhyqy5 closed 5 years ago

lhyqy5 commented 5 years ago

When logging a self-referencing loop object, the program just aborted with a message 'Process is terminating due to StackOverflowException'.

It's seems caused by serializing in ElasticSearch.Net.PostData

I found there is an ElasticsearchSerializer property in target config, I wonder know how can I config this property to use my custom Serializer in the nlog config file.

snakefoot commented 5 years ago

ReferenceLoopHandling is configured:

https://github.com/markmcdowell/NLog.Targets.ElasticSearch/blob/0e78f96d3f5b4b5890d92b7c2ef24da601ec15ef/src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs#L271

lhyqy5 commented 5 years ago

this config does not affect Elasticsearch's default serializer Elasticsearch.Net.LowLevelRequestResponseSerializer which use Elasticsearch.Net.SimpleJson

snakefoot commented 5 years ago

Looks like SimpleJson is being replaced https://github.com/elastic/elasticsearch-net/pull/3608

snakefoot commented 5 years ago

If you are very good, then you can setup dependency-injection with NLog, so when NLog loads the nlog.config, then you can override the creation of the ElasticSearchTarget-object (and assign your custom implementation of IElasticsearchSerializer):

var defaultConstructor = NLog.Config.ConfigurationItemFactory.Default.CreateInstance;
NLog.Config.ConfigurationItemFactory.Default.CreateInstance.CreateInstance = type =>
{
   if (type == typeof(ElasticSearchTarget))
       return new ElasticSearchTarget() { ElasticsearchSerializer = myFunkySerializer };

   return defaultConstructor(type);
};

https://github.com/NLog/NLog/wiki/Dependency-injection-with-NLog

Just make sure to setup the factory-logic before loading the NLog-config. Or perform a reload of the NLog-config after having setup the factory-logic.

lhyqy5 commented 5 years ago

I have tried your solution, it works well, Thanks.