LoggerMessage.LogValues<T> throws execption when calling [0] #410
Description
We created an own ILogger and iterate thru each KeyValuePair in the state object.
public class CustomLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var structure = state as IReadOnlyList<KeyValuePair<string, object>>;
if (structure == null)
{
return;
}
foreach (var pair in structure)
{
// do something
}
}
}
We are also using the JwtBearer token authentication. Each time when the authentication is successful a Logger.Define() without any paramter is called.
When iterating thru the ReadOnlyList an ArgumentOutOfRangeException will be thrown because the count of _formatter.ValueNames is zero.
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parametername: index
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
Microsoft.Extensions.Logging.LoggerMessage.LogValues`1.get_Item(Int32 index)
Submission#12.CustomLogger.Log(Microsoft.Extensions.Logging.LogLevel, Microsoft.Extensions.Logging.EventId, TState, System.Exception, Func<TState, System.Exception, string>)
var logger = new CustomLogger();
var logMessage = LoggerMessage.Define<string>(
eventId: 2,
logLevel: LogLevel.Information,
formatString: "Successfully validated the token.");
logMessage(logger, null, null);