From f297dc55ad069b54c363606a5cc7b4d3b20585c1 Mon Sep 17 00:00:00 2001 From: mbrzozowski Date: Thu, 17 Oct 2019 17:46:12 +0200 Subject: [PATCH] Fix log events not being written to Sink when exception has no stacktrace A null reference exception was thrown when trying to format Exception with no stacktrace (either root or inner one). This caused log events to not be written to Sink. --- .../ElasticsearchJsonFormatter.cs | 2 +- .../ElasticsearchJsonFormatterTests.cs | 37 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Serilog.Formatting.Elasticsearch/ElasticsearchJsonFormatter.cs b/src/Serilog.Formatting.Elasticsearch/ElasticsearchJsonFormatter.cs index 13cf47c5..b0187050 100644 --- a/src/Serilog.Formatting.Elasticsearch/ElasticsearchJsonFormatter.cs +++ b/src/Serilog.Formatting.Elasticsearch/ElasticsearchJsonFormatter.cs @@ -247,7 +247,7 @@ protected void WriteSingleException(Exception exception, ref string delim, TextW private void WriteMultilineString(string name, string value, ref string delimeter, TextWriter output) { - string[] lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + var lines = value?.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) ?? new string[] { }; WriteJsonArrayProperty(name, lines, ref delimeter, output); } diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchJsonFormatterTests.cs b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchJsonFormatterTests.cs index 6a2e1c2d..d62a611f 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchJsonFormatterTests.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchJsonFormatterTests.cs @@ -19,7 +19,7 @@ static LogEvent CreateLogEvent() => DateTimeOffset.Now, LogEventLevel.Debug, //exception: CreateThrownException(), - exception: null, + exception: null, messageTemplate: new MessageTemplate(Enumerable.Empty()), properties: Enumerable.Empty() ); @@ -55,12 +55,26 @@ static void ThrowInnerException() throw new Exception("Test inner exception message"); } - static LogEvent CreateLogEventWithException() => + static Exception CreateThrownExceptionWithNotThrownInner() + { + Exception retEx = null; + try + { + throw new Exception("Test exception message", new Exception("Test inner exception message")); + } + catch (Exception ex) + { + retEx = ex; + } + return retEx; + } + + static LogEvent CreateLogEventWithException(Exception ex) => new LogEvent ( DateTimeOffset.Now, LogEventLevel.Debug, - exception: CreateThrownException(), + exception: ex, messageTemplate: new MessageTemplate(Enumerable.Empty()), properties: Enumerable.Empty() ); @@ -181,7 +195,7 @@ public void When_provide_closing_delimiter_should_use_it() public void DefaultJsonFormater_Should_Render_Exceptions() { CheckProperties( - CreateLogEventWithException, + () => CreateLogEventWithException(CreateThrownException()), new ElasticsearchJsonFormatter(), result => { @@ -205,11 +219,12 @@ public void DefaultJsonFormater_Should_Render_Exceptions() }); } - [Fact] - public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Array() + [Theory] + [MemberData(nameof(TestExceptions))] + public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Array(Exception exception) { CheckProperties( - CreateLogEventWithException, + () => CreateLogEventWithException(exception), new ElasticsearchJsonFormatter(formatStackTraceAsArray: true), result => { @@ -232,5 +247,13 @@ public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Arra Assert.StartsWith("[", stackTraceValue); }); } + + public static IEnumerable TestExceptions => + new List + { + new object[] { CreateThrownException() }, + new object[] { CreateThrownExceptionWithNotThrownInner() }, + new object[] { new Exception("Not thrown exception message") } + }; } }