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

LoggerMessage.LogValues<T> throws execption when calling [0] #410

Closed
@sreicht

Description

@sreicht

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.

https://github.com/aspnet/Security/blob/4d6ad51f8a9354d8e62cf8d66db97bd991b7d93c/src/Microsoft.AspNetCore.Authentication.JwtBearer/LoggingExtensions.cs#L37

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>)

https://github.com/aspnet/Logging/blob/release/src/Microsoft.Extensions.Logging.Abstractions/LoggerMessage.cs#L298

var logger = new CustomLogger();
var logMessage = LoggerMessage.Define<string>(
                 eventId: 2,
                 logLevel: LogLevel.Information,
                 formatString: "Successfully validated the token.");
logMessage(logger, null, null);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions