Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Fix case where no args passed to Log* extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Jan 29, 2016
1 parent 4528bdb commit 6e8bcd9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,38 @@ public class FormattedLogValues : IReadOnlyList<KeyValuePair<string, object>>
private static ConcurrentDictionary<string, LogValuesFormatter> _formatters = new ConcurrentDictionary<string, LogValuesFormatter>();
private readonly LogValuesFormatter _formatter;
private readonly object[] _values;
private readonly string _originalMessage;

public FormattedLogValues(string format, params object[] values)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));

if (values.Length != 0)
{
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));
}

_originalMessage = format;
_values = values;
}

public KeyValuePair<string, object> this[int index]
{
get
{
if (index > Count)
if (index < 0 || index >= Count)
{
throw new IndexOutOfRangeException(nameof(index));
}

if (index == Count - 1)
{
return new KeyValuePair<string, object> ("{OriginalFormat}", _originalMessage);
}

return _formatter.GetValue(_values, index);
}
}
Expand All @@ -44,6 +57,11 @@ public int Count
{
get
{
if (_formatter == null)
{
return 1;
}

return _formatter.ValueNames.Count + 1;
}
}
Expand All @@ -58,6 +76,11 @@ public IEnumerator<KeyValuePair<string, object>> GetEnumerator()

public override string ToString()
{
if (_formatter == null)
{
return _originalMessage;
}

return _formatter.Format(_values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public string Format(object[] values)

public KeyValuePair<string, object> GetValue(object[] values, int index)
{
if (index < 0 || index > _valueNames.Count + 1)
if (index < 0 || index > _valueNames.Count)
{
throw new IndexOutOfRangeException(nameof(index));
}
Expand Down
10 changes: 5 additions & 5 deletions test/Microsoft.Extensions.Logging.Test/FormattedLogValuesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void LogValues_With_DateTime(string expected, string format)
}

[Theory]
[InlineData("{", "{{", null)]
[InlineData("'{'", "'{{'", null)]
[InlineData("'{}'", "'{{}}'", null)]
[InlineData("{{", "{{", null)]
[InlineData("'{{'", "'{{'", null)]
[InlineData("'{{}}'", "'{{}}'", null)]
[InlineData("arg1 arg2 '{}' '{' '{:}' '{,:}' {,}- test string",
"{0} {1} '{{}}' '{{' '{{:}}' '{{,:}}' {{,}}- test string",
new object[] { "arg1", "arg2" })]
Expand All @@ -62,12 +62,12 @@ public void LogValues_With_Escaped_Braces(string expected, string format, object
[Theory]
[InlineData("{foo")]
[InlineData("bar}")]
[InlineData("{foo bar}")]
[InlineData("{foo bar}}")]
public void LogValues_With_UnbalancedBraces(string format)
{
Assert.Throws<FormatException>(() =>
{
var logValues = new FormattedLogValues(format);
var logValues = new FormattedLogValues(format, new object[] { "arg1" });
logValues.ToString();
});
}
Expand Down

0 comments on commit 6e8bcd9

Please sign in to comment.