88using System . Diagnostics . CodeAnalysis ;
99using System . Linq ;
1010using Microsoft . Extensions . DependencyInjection ;
11+ using Microsoft . Extensions . Logging . Abstractions ;
1112using Microsoft . Extensions . Options ;
1213
1314namespace Microsoft . Extensions . Logging
@@ -211,12 +212,19 @@ private void AddProviderRegistration(ILoggerProvider provider, bool dispose)
211212
212213 private LoggerInformation [ ] CreateLoggers ( string categoryName )
213214 {
214- var loggers = new LoggerInformation [ _providerRegistrations . Count ] ;
215+ var loggers = new List < LoggerInformation > ( _providerRegistrations . Count ) ;
215216 for ( int i = 0 ; i < _providerRegistrations . Count ; i ++ )
216217 {
217- loggers [ i ] = new LoggerInformation ( _providerRegistrations [ i ] . Provider , categoryName ) ;
218+ var loggerInformation = new LoggerInformation ( _providerRegistrations [ i ] . Provider , categoryName ) ;
219+
220+ // We do not need to check for NullLogger<T>.Instance as no provider would reasonably return it (the <T> handling is at
221+ // outer loggers level, not inner level loggers in Logger/LoggerProvider).
222+ if ( loggerInformation . Logger != NullLogger . Instance )
223+ {
224+ loggers . Add ( loggerInformation ) ;
225+ }
218226 }
219- return loggers ;
227+ return [ .. loggers ] ;
220228 }
221229
222230 private ( MessageLogger [ ] MessageLoggers , ScopeLogger [ ] ? ScopeLoggers ) ApplyFilters ( LoggerInformation [ ] loggers )
@@ -237,6 +245,14 @@ private LoggerInformation[] CreateLoggers(string categoryName)
237245 continue ;
238246 }
239247
248+ var logger = loggerInformation . Logger ;
249+ if ( logger is NullLogger ||
250+ ( logger . GetType ( ) is var loggerType && loggerType . IsGenericType && loggerType . GetGenericTypeDefinition ( ) == typeof ( NullLogger < > ) ) )
251+ {
252+ // Skip NullLogger instances
253+ continue ;
254+ }
255+
240256 messageLoggers . Add ( new MessageLogger ( loggerInformation . Logger , loggerInformation . Category , loggerInformation . ProviderType . FullName , minLevel , filter ) ) ;
241257
242258 if ( ! loggerInformation . ExternalScope )
0 commit comments