Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle IndexOutOfRangeException thrown by Microsoft.Extensions.Logging when using invalid format string #272

Merged
merged 1 commit into from
Mar 27, 2019
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
16 changes: 12 additions & 4 deletions src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,27 @@ private static bool IsValidParameterList(IReadOnlyList<KeyValuePair<string, obje
bool? firstParameterIsPositional = null;
for (int i = 0; i < parameterList.Count; ++i)
{
var paramPair = parameterList[i];
if (string.IsNullOrEmpty(paramPair.Key))
string parameterKey;
try
{
parameterKey = parameterList[i].Key;
}
catch (IndexOutOfRangeException ex)
{
throw new FormatException($"Invalid format string. Expected {parameterList.Count - 1} format parameters, but failed to lookup parameter index {i}", ex);
}
if (string.IsNullOrEmpty(parameterKey))
{
originalMessageIndex = null;
return false;
}

char firstChar = paramPair.Key[0];
char firstChar = parameterKey[0];
if (GetCaptureType(firstChar) != CaptureType.Normal)
{
hasMessageTemplateCapture = true;
}
else if (paramPair.Key == NLogLogger.OriginalFormatPropertyName)
else if (parameterKey == NLogLogger.OriginalFormatPropertyName)
{
if (originalMessageIndex.HasValue)
{
Expand Down
30 changes: 23 additions & 7 deletions test/NLog.Extensions.Logging.Tests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ public void TestScopePropertyDictionary()
Assert.Equal("NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message with id and 1 parameters |Hello", target.Logs.LastOrDefault());
}

[Fact]
public void TestInvalidFormatString()
{
var runner = GetRunner<Runner>();
var ex = Assert.Throws<AggregateException>(() => runner.Log(Microsoft.Extensions.Logging.LogLevel.Information, 0, null, "{0}{1}", "Test"));
Assert.IsType<FormatException>(ex.InnerException);
}

[Fact]
public void TestInvalidFormatString2()
{
var runner = GetRunner<Runner>(new NLogProviderOptions() { CaptureMessageTemplates = false });
var ex = Assert.Throws<AggregateException>(() => runner.Log(Microsoft.Extensions.Logging.LogLevel.Information, 0, null, "{0}{1}", "Test"));
Assert.IsType<FormatException>(ex.InnerException);
}

[Theory]
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL|message Exception of type 'System.Exception' was thrown.|20")]
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message Exception of type 'System.Exception' was thrown.|20")]
Expand Down Expand Up @@ -231,27 +247,27 @@ public void LogDebugWithId()
_logger.LogDebug(20, "message with id");
}

public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, int eventId, Exception exception, string message)
public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, int eventId, Exception exception, string message, params object[] args)
{
switch (logLevel)
{
case Microsoft.Extensions.Logging.LogLevel.Trace:
_logger.LogTrace(eventId, exception, message);
_logger.LogTrace(eventId, exception, message, args);
break;
case Microsoft.Extensions.Logging.LogLevel.Debug:
_logger.LogDebug(eventId, exception, message);
_logger.LogDebug(eventId, exception, message, args);
break;
case Microsoft.Extensions.Logging.LogLevel.Information:
_logger.LogInformation(eventId, exception, message);
_logger.LogInformation(eventId, exception, message, args);
break;
case Microsoft.Extensions.Logging.LogLevel.Warning:
_logger.LogWarning(eventId, exception, message);
_logger.LogWarning(eventId, exception, message, args);
break;
case Microsoft.Extensions.Logging.LogLevel.Error:
_logger.LogError(eventId, exception, message);
_logger.LogError(eventId, exception, message, args);
break;
case Microsoft.Extensions.Logging.LogLevel.Critical:
_logger.LogCritical(eventId, exception, message);
_logger.LogCritical(eventId, exception, message, args);
break;
default:
throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
Expand Down