Plugin logging was not working because the plugin loader was unable to inject ILogger dependencies into plugin constructors, causing all plugins to fall back to parameterless constructors where _logger remained null.
- Constructor Mismatch: Plugin constructors expected
ILogger<T>(generic typed logger) but the plugin loader was trying to createILogger(non-generic) - Failed Reflection Call: The reflection code
typeof(ILoggerFactory).GetMethod("CreateLogger", new Type[0])was looking for a parameterless method but the genericCreateLogger<T>()method has different metadata - Type Incompatibility: Even when logger creation worked,
ILoggerFactory.CreateLogger(string)returnsILoggerbut plugins expectedILogger<PluginType>
Added support for both generic and non-generic ILogger constructors:
public PinValidationPlugin(ILogger<PinValidationPlugin> logger) : this()
{
_logger = logger;
}
// Constructor for non-generic ILogger (used by plugin loader)
public PinValidationPlugin(ILogger logger) : this()
{
_logger = logger;
}Changed from strongly-typed to generic interface:
// Before
private readonly ILogger<PinValidationPlugin>? _logger;
// After
private readonly ILogger? _logger;Updated CachedPluginLoader.CreatePluginInstance() to:
// Look for constructors that take ILogger<T> or ILogger
var genericLoggerType = typeof(ILogger<>).MakeGenericType(pluginType);
var nonGenericLoggerType = typeof(ILogger);
var constructorWithLogger = pluginType.GetConstructor(new[] { genericLoggerType })
?? pluginType.GetConstructor(new[] { nonGenericLoggerType });
// Create logger using string-based method
var stringLogger = _loggerFactory.CreateLogger(pluginType.FullName ?? pluginType.Name);
var plugin = (IApBoxPlugin?)Activator.CreateInstance(pluginType, stringLogger);src/ApBox.Plugins/CachedPluginLoader.cs- Enhanced plugin instantiation logicsrc/ApBox.Plugins/PluginLoader.cs- Same fix for base plugin loadersrc/ApBox.SamplePlugins/PinValidationPlugin.cs- Added non-generic logger constructorsrc/ApBox.Web/Services/ServiceCollectionExtensions.cs- Added ILoggerFactory injection
- ✅ Plugins now receive proper logger injection
- ✅ All plugin logging works correctly
- ✅ Backward compatible with existing plugin constructors
- ✅ Two-factor authentication demo shows detailed logging flow
- Apply the plugin loader changes to support both
ILogger<T>andILoggerconstructors - Update all sample plugins to include non-generic
ILoggerconstructor overloads - Consider making this the standard pattern for new plugins to ensure consistent logging
- Add unit tests to verify logger injection works for both constructor types
This fix enables proper debugging and monitoring of plugin behavior in production environments.