diff --git a/src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs b/src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs index d71bfc0d123..2a3d04b8188 100644 --- a/src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs +++ b/src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs @@ -110,6 +110,11 @@ private LoadBalancedCluster CreateLoadBalancedCluster(ClusterSettings setting) private void ProcessClusterEnvironment(ClusterSettings settings) { + if (_loggerFactory == null) + { + return; + } + foreach (var (host, _) in settings.EndPoints.Select(EndPointHelper.GetHostAndPort)) { if (LogIfCosmosDB(host) || LogIfDocumentDB(host)) @@ -129,7 +134,7 @@ bool LogIfExternalEnvironment(string host, string environment, string documentat if (suffixes.Any(s => host.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))) { var logger = _loggerFactory.CreateLogger(); - logger.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl); + logger?.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl); return true; } diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterFactoryTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterFactoryTests.cs index ab4f1ff375c..ae2f67a00ee 100644 --- a/tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterFactoryTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterFactoryTests.cs @@ -35,6 +35,16 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output) { } + [Fact] + public void ClusterFactory_should_create_cluster_when_loggerfactory_is_not_set() + { + const string connectionString = "mongodb://a.MONGO.COSMOS.AZURE.COM:19555"; + var subject = CreateSubject(connectionString, null); + var cluster = subject.CreateCluster(); + + cluster.Should().NotBeNull(); + } + [Theory] [InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)] [InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)] @@ -59,7 +69,7 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output) [InlineData("mongodb://a.mongo.cosmos.azure.com:19554,b.docdb-elastic.amazonaws.com:27017,c.mongo.cosmos.azure.com:19555/", ExpectedCosmosDBMessage)] public void ClusterFactory_should_log_if_external_environment_is_detected(string connectionString, string expectedMessage) { - var subject = CreateSubject(connectionString); + var subject = CreateSubject(connectionString, LoggerFactory); _ = subject.CreateCluster(); var logs = GetLogs(); @@ -77,14 +87,14 @@ public void ClusterFactory_should_log_if_external_environment_is_detected(string [InlineData("mongodb+srv://a.docdb-elastic.amazonaws.com.tld/")] public void ClusterFactory_should_not_log_if_no_external_environment_is_detected(string connectionString) { - var subject = CreateSubject(connectionString); + var subject = CreateSubject(connectionString, LoggerFactory); _ = subject.CreateCluster(); var logs = GetLogs(); logs.Length.Should().Be(0); } - private ClusterFactory CreateSubject(string connectionString) + private ClusterFactory CreateSubject(string connectionString, ILoggerFactory loggerFactory) { var parsedConnectionString = new ConnectionString(connectionString); @@ -92,7 +102,7 @@ private ClusterFactory CreateSubject(string connectionString) var serverFactoryMock = Mock.Of(); var clusterSettings = new ClusterSettings(endPoints: Optional.Enumerable(parsedConnectionString.Hosts)); - var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, LoggerFactory); + var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, loggerFactory); return clusterFactory; }