Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Fix log events not being written to Sink when exception has no stacktrace #285

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static LogEvent CreateLogEvent() =>
DateTimeOffset.Now,
LogEventLevel.Debug,
//exception: CreateThrownException(),
exception: null,
exception: null,
messageTemplate: new MessageTemplate(Enumerable.Empty<MessageTemplateToken>()),
properties: Enumerable.Empty<LogEventProperty>()
);
Expand Down Expand Up @@ -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<MessageTemplateToken>()),
properties: Enumerable.Empty<LogEventProperty>()
);
Expand Down Expand Up @@ -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 =>
{
Expand All @@ -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 =>
{
Expand All @@ -232,5 +247,13 @@ public void DefaultJsonFormater_Should_Render_Exceptions_With_StackTrace_As_Arra
Assert.StartsWith("[", stackTraceValue);
});
}

public static IEnumerable<object[]> TestExceptions =>
new List<object[]>
{
new object[] { CreateThrownException() },
new object[] { CreateThrownExceptionWithNotThrownInner() },
new object[] { new Exception("Not thrown exception message") }
};
}
}