AddProvider checks for non-null provider

Microsoft.Extensions.Logging.LoggerFactory implements ILoggerFactory with an AddProvider(ILoggerProvider) method. null providers aren't accepted and will cause an ArgumentNullException to be thrown.

Version introduced

6.0 RC 1

Previous behavior

Previously, AddProvider(ILoggerProvider) did not perform any validation of the provider argument. As such, the method considered null to be a "valid" provider and added it to the collection of providers.

New behavior

Starting in .NET 6, null providers aren't accepted, and AddProvider(ILoggerProvider) throws an ArgumentNullException if the logging provider argument is null. For example, the following code throws an ArgumentNullException:

var factory = new LoggerFactory();
((ILoggerFactory)factory).AddProvider(null));

Type of breaking change

This change can affect source compatibility.

Reason for change

The previous behavior caused some operations inside the class to unnecessarily throw NullReferenceException exceptions. For example, the LoggerFactory.Dispose() method will capture the exception and do nothing.

Ensure you're not passing a null provider to AddProvider(ILoggerProvider).

Affected APIs