-2

Some time ago I injected NLogs for ILogger see the link bellow.

Prism Unity Register NLog as Microsoft.Extension.Logging.ILogger<T>

No some time after I recongnized that the Classname is wrong. It is always pointing to my Extension instead of the actual class the log is comming from.

2025-06-14 14:55:34.1914|ERROR|14|Microsoft.Extensions.Logging.LoggerExtensions.Log| Error during loading TestClass |System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. (Parameter 'startIndex') at System.String.ThrowSubstringArgumentOutOfRange(Int32 startIndex, Int32 length)

ClassXY.Function() |System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. (Parameter 'startIndex') at System.String.ThrowSubstringArgumentOutOfRange(Int32 startIndex, Int32 length)

Does anyone know how this can happen.

1 Answer 1

0

The answer was simple as Rolf Kristensen already noticed the NLogLoggerFactory creates a NLogger itself. Therefore it isn't needed to apply one self.
In my case it wasn't only not neccessary but also lead to the error seen above.

Before:

public static IContainerRegistry AddLogger(this IContainerRegistry services)
{
    var loggerFactory = new NLogLoggerFactory();
    var provider = new NLogLoggerProvider();
    loggerFactory.AddProvider(provider);
    services.RegisterInstance<ILoggerFactory>(loggerFactory);
    services.Register(typeof(ILogger<>), typeof(Logger<>));
    return services;
}

After:


public static IContainerRegistry AddLogger(this IContainerRegistry services)
{
    var loggerFactory = new NLogLoggerFactory();
    services.RegisterInstance<ILoggerFactory>(loggerFactory);
    services.Register(typeof(ILogger<>), typeof(Logger<>));
    return services;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.