Skip to content

Commit

Permalink
Add exception message to formatting error log
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jun 26, 2019
1 parent 100d615 commit 0a3de34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/ZeroLog.Tests/LogManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void should_not_throw_if_formatting_fails_when_using_format_string()
signal.Wait(TimeSpan.FromMilliseconds(100));

var logMessage = _testAppender.LoggedMessages.Single();
Check.That(logMessage).Equals("An error occured during formatting: \"A good format: {0:X4}, A bad format: {1:lol}, Another good format: {2}\", -23805, " + guid + ", True");
Check.That(logMessage).Equals("An error occured during formatting: Unknown format specifier 'lol'. - Arguments: \"A good format: {0:X4}, A bad format: {1:lol}, Another good format: {2}\", -23805, " + guid + ", True");
}

[Test]
Expand Down Expand Up @@ -229,7 +229,7 @@ public unsafe void should_not_throw_if_formatting_fails_when_appending_formatted
signal.Wait(TimeSpan.FromMilliseconds(100));

var logMessage = _testAppender.LoggedMessages.Single();
Check.That(logMessage).Equals("An error occured during formatting: \"Hello\", False, 1, 'a', 2, 3, 4, 5, 6, 7, " + guid + ", 2017-02-24 16:51:51.000, 16:51:51, \"abc\", \"abc\", Friday");
Check.That(logMessage).Equals("An error occured during formatting: Unknown format specifier 'meh, this is going to break formatting'. - Arguments: \"Hello\", False, 1, 'a', 2, 3, 4, 5, 6, 7, " + guid + ", 2017-02-24 16:51:51.000, 16:51:51, \"abc\", \"abc\", Friday");
}

[Test]
Expand All @@ -247,7 +247,7 @@ public void should_write_unformatted_unmanaged_struct_when_formatting_fails()
signal.Wait(TimeSpan.FromMilliseconds(100));

var logMessage = _testAppender.LoggedMessages.Single();
Check.That(logMessage).Equals("An error occured during formatting: Unmanaged(0x2a000000)");
Check.That(logMessage).Equals("An error occured during formatting: Simulated failure - Arguments: Unmanaged(0x2a000000)");
}

[Test]
Expand Down
8 changes: 5 additions & 3 deletions src/ZeroLog/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ private bool TryToProcessQueue(StringBuffer stringBuffer, byte[] destination)
FormatLogMessage(stringBuffer, logEvent);
bytesWritten = CopyStringBufferToByteArray(stringBuffer, destination);
}
catch
catch (Exception ex)
{
HandleFormattingError(stringBuffer, logEvent, destination, out bytesWritten);
HandleFormattingError(stringBuffer, logEvent, destination, ex, out bytesWritten);
}

WriteMessageLogToAppenders(destination, logEvent, bytesWritten);
Expand All @@ -292,12 +292,14 @@ private bool TryToProcessQueue(StringBuffer stringBuffer, byte[] destination)
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void HandleFormattingError(StringBuffer stringBuffer, IInternalLogEvent logEvent, byte[] destination, out int bytesWritten)
private static void HandleFormattingError(StringBuffer stringBuffer, IInternalLogEvent logEvent, byte[] destination, Exception exception, out int bytesWritten)
{
try
{
stringBuffer.Clear();
stringBuffer.Append("An error occured during formatting: ");
stringBuffer.Append(exception.Message);
stringBuffer.Append(" - Arguments: ");

logEvent.WriteToStringBufferUnformatted(stringBuffer);
bytesWritten = CopyStringBufferToByteArray(stringBuffer, destination);
Expand Down

0 comments on commit 0a3de34

Please sign in to comment.