diff --git a/src/Elastic.Serilog.Sinks/ElasticsearchSink.cs b/src/Elastic.Serilog.Sinks/ElasticsearchSink.cs index fe4fc3f8..79653885 100644 --- a/src/Elastic.Serilog.Sinks/ElasticsearchSink.cs +++ b/src/Elastic.Serilog.Sinks/ElasticsearchSink.cs @@ -152,9 +152,12 @@ public SelfLogCallbackListener(ElasticsearchSinkOptions options) }; ExportResponseCallback = (response, _) => { - var errorItems = response.Items.Where(i => i.Status >= 300).ToList(); + if (response == null) return; + if (response.TryGetElasticsearchServerError(out var error)) SelfLog.WriteLine("{0}", error); + // ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract + var errorItems = response.Items?.Where(i => i.Status >= 300).ToArray() ?? Array.Empty(); foreach (var errorItem in errorItems) SelfLog.WriteLine("{0}", $"Failed to {errorItem.Action} document status: ${errorItem.Status}, error: ${errorItem.Error}");