Skip to content

Commit f41c21e

Browse files
authored
CSHARP-4870: CreateCluster throws NullReferenceException when not setting LoggingSettings (#1236)
1 parent 136ee95 commit f41c21e

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ private LoadBalancedCluster CreateLoadBalancedCluster(ClusterSettings setting)
110110

111111
private void ProcessClusterEnvironment(ClusterSettings settings)
112112
{
113+
if (_loggerFactory == null)
114+
{
115+
return;
116+
}
117+
113118
foreach (var (host, _) in settings.EndPoints.Select(EndPointHelper.GetHostAndPort))
114119
{
115120
if (LogIfCosmosDB(host) || LogIfDocumentDB(host))
@@ -129,7 +134,7 @@ bool LogIfExternalEnvironment(string host, string environment, string documentat
129134
if (suffixes.Any(s => host.EndsWith(s, StringComparison.InvariantCultureIgnoreCase)))
130135
{
131136
var logger = _loggerFactory.CreateLogger<LogCategories.Client>();
132-
logger.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl);
137+
logger?.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl);
133138

134139
return true;
135140
}

tests/MongoDB.Driver.Core.Tests/Core/Clusters/ClusterFactoryTests.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output)
3535
{
3636
}
3737

38+
[Fact]
39+
public void ClusterFactory_should_create_cluster_when_loggerfactory_is_not_set()
40+
{
41+
const string connectionString = "mongodb://a.MONGO.COSMOS.AZURE.COM:19555";
42+
var subject = CreateSubject(connectionString, null);
43+
var cluster = subject.CreateCluster();
44+
45+
cluster.Should().NotBeNull();
46+
}
47+
3848
[Theory]
3949
[InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)]
4050
[InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)]
@@ -59,7 +69,7 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output)
5969
[InlineData("mongodb://a.mongo.cosmos.azure.com:19554,b.docdb-elastic.amazonaws.com:27017,c.mongo.cosmos.azure.com:19555/", ExpectedCosmosDBMessage)]
6070
public void ClusterFactory_should_log_if_external_environment_is_detected(string connectionString, string expectedMessage)
6171
{
62-
var subject = CreateSubject(connectionString);
72+
var subject = CreateSubject(connectionString, LoggerFactory);
6373
_ = subject.CreateCluster();
6474

6575
var logs = GetLogs();
@@ -77,22 +87,22 @@ public void ClusterFactory_should_log_if_external_environment_is_detected(string
7787
[InlineData("mongodb+srv://a.docdb-elastic.amazonaws.com.tld/")]
7888
public void ClusterFactory_should_not_log_if_no_external_environment_is_detected(string connectionString)
7989
{
80-
var subject = CreateSubject(connectionString);
90+
var subject = CreateSubject(connectionString, LoggerFactory);
8191
_ = subject.CreateCluster();
8292

8393
var logs = GetLogs();
8494
logs.Length.Should().Be(0);
8595
}
8696

87-
private ClusterFactory CreateSubject(string connectionString)
97+
private ClusterFactory CreateSubject(string connectionString, ILoggerFactory loggerFactory)
8898
{
8999
var parsedConnectionString = new ConnectionString(connectionString);
90100

91101
var eventSubscriberMock = Mock.Of<IEventSubscriber>();
92102
var serverFactoryMock = Mock.Of<IClusterableServerFactory>();
93103

94104
var clusterSettings = new ClusterSettings(endPoints: Optional.Enumerable(parsedConnectionString.Hosts));
95-
var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, LoggerFactory);
105+
var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, loggerFactory);
96106

97107
return clusterFactory;
98108
}

0 commit comments

Comments
 (0)