serilog-contrib / serilog-sinks-elasticsearch

A Serilog sink that writes events to Elasticsearch
Apache License 2.0
434 stars 197 forks source link

The given key 'error' was not present in the dictionary. #268

Closed nvivo closed 1 year ago

nvivo commented 5 years ago

Some events are missing from ElasticSearch and I'm investigating what is happening. Looking into the selflog I get this error every few minutes:

2019-08-23T17:40:38.4905634Z Exception while emitting periodic batch from Serilog.Sinks.Elasticsearch.ElasticsearchSink: System.Collections.Generic.KeyNotFoundException: The given key 'error' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Serilog.Sinks.Elasticsearch.ElasticsearchSink.EmitBatch(IEnumerable`1 events)
   at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(IEnumerable`1 events)
   at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()

Is this a bug or I might be causing it from my code?

mivano commented 5 years ago

Can you give it a go with the latest alpha nuget package? There was some fix in the dynamic error message that is returned from the batch operation.

Op 23 aug. 2019 om 20:21 heeft Natan Vivo notifications@github.com het volgende geschreven:

Some events are missing from ElasticSearch and I'm investigating what is happening. Looking into the selflog I get this error every few minutes:

2019-08-23T17:40:38.4905634Z Exception while emitting periodic batch from Serilog.Sinks.Elasticsearch.ElasticsearchSink: System.Collections.Generic.KeyNotFoundException: The given key 'error' was not present in the dictionary. at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Serilog.Sinks.Elasticsearch.ElasticsearchSink.EmitBatch(IEnumerable1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(IEnumerable`1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick() Is this a bug or I might be causing it from my code?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

nvivo commented 5 years ago

I just got the latest dev branch and ran with debug symbols.

The issue is here: https://github.com/serilog/serilog-sinks-elasticsearch/blob/9d9930e2e5e960dc8c1b230fc7eaaac5c84e3dc9/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs#L75

Looks like the ["error"] is not present. Should it always be there?

nvivo commented 5 years ago

Ok, I got what is happening.

The response from ElasticSearch contains all items, not only the ones with errors. This is part of the response:

image

While DynamicResponse uses TryGetValue for the "index" part, the value itself is another dictionary and that one complains about trying to get "error" without a check.

mivano commented 5 years ago

Hmm, that is unfortunate, I recently merged this one (https://github.com/serilog/serilog-sinks-elasticsearch/pull/266/files) into dev. So if you tried the dev branch, then it still has issues. Would you care for a PR?

mivano commented 5 years ago

See #271

mivano commented 4 years ago

Is this still an issue with the latest nuget package?

estoces commented 4 years ago

I was having the same issue with 8.0.0. Updated to alpha0002 and no longer have the problem.