Skip to content

CSHARP-4870: CreateCluster throws NullReferenceException when not setting LoggingSettings #1236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<LogCategories.Client>();
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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();
Expand All @@ -77,22 +87,22 @@ 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);

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

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;
}
Expand Down