diff --git a/src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs b/src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs index 97408ac..1d24d5b 100644 --- a/src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs +++ b/src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs @@ -5,6 +5,8 @@ using NLog.Common; using NLog.Config; using NLog.Layouts; +using Newtonsoft.Json; +using System.Dynamic; namespace NLog.Targets.ElasticSearch { @@ -115,6 +117,10 @@ private void SendBatch(IEnumerable events) InternalLogger.Error("Failed to send log messages to elasticsearch: status={0}, message=\"{1}\"", result.HttpStatusCode, result.OriginalException?.Message ?? "No error message. Enable Trace logging for more information."); InternalLogger.Trace("Failed to send log messages to elasticsearch: result={0}", result); + + //rethow exception for fallbackgroup setup. + if (result.OriginalException != null) + throw result.OriginalException; } catch (Exception ex) { @@ -138,8 +144,11 @@ private object FormPayload(IEnumerable logEvents) {"message", Layout.Render(logEvent)} }; + //see : https://github.com/elastic/elasticsearch-net/issues/2052 + var ex = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(logEvent.Exception)); + if (logEvent.Exception != null) - document.Add("exception", logEvent.Exception); + document.Add("exception", ex); foreach (var field in Fields) { diff --git a/src/NLog.Targets.ElasticSearch/NLog.Targets.ElasticSearch.csproj b/src/NLog.Targets.ElasticSearch/NLog.Targets.ElasticSearch.csproj index 7a4466e..6d44ec1 100644 --- a/src/NLog.Targets.ElasticSearch/NLog.Targets.ElasticSearch.csproj +++ b/src/NLog.Targets.ElasticSearch/NLog.Targets.ElasticSearch.csproj @@ -38,6 +38,10 @@ True + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + True + ..\packages\NLog.4.2.3\lib\net45\NLog.dll True diff --git a/src/NLog.Targets.ElasticSearch/packages.config b/src/NLog.Targets.ElasticSearch/packages.config index d706439..6a42cb7 100644 --- a/src/NLog.Targets.ElasticSearch/packages.config +++ b/src/NLog.Targets.ElasticSearch/packages.config @@ -1,5 +1,6 @@  + \ No newline at end of file