Skip to content

Commit f87f32b

Browse files
author
Scott Bommarito
authored
Use IDiagnosticsSource for automatic RequireOrganizationTenantPolicy logs (#6871)
1 parent 118c55f commit f87f32b

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

src/NuGetGallery/Services/UserService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using NuGet.Services.Entities;
1313
using NuGetGallery.Auditing;
1414
using NuGetGallery.Configuration;
15+
using NuGetGallery.Diagnostics;
1516
using NuGetGallery.Infrastructure.Authentication;
1617
using NuGetGallery.Security;
1718
using Crypto = NuGetGallery.CryptographyService;
@@ -40,7 +41,7 @@ public class UserService : IUserService
4041

4142
public ITelemetryService TelemetryService { get; protected set; }
4243

43-
public ILogger<UserService> Logger { get; protected set; }
44+
public IDiagnosticsSource DiagnosticsSource { get; protected set; }
4445

4546
protected UserService() { }
4647

@@ -56,7 +57,7 @@ public UserService(
5657
IDateTimeProvider dateTimeProvider,
5758
ICredentialBuilder credentialBuilder,
5859
ITelemetryService telemetryService,
59-
ILogger<UserService> logger)
60+
IDiagnosticsService diagnosticsService)
6061
: this()
6162
{
6263
Config = config;
@@ -69,7 +70,7 @@ public UserService(
6970
SecurityPolicyService = securityPolicyService;
7071
DateTimeProvider = dateTimeProvider;
7172
TelemetryService = telemetryService;
72-
Logger = logger;
73+
DiagnosticsSource = diagnosticsService.SafeGetSource(nameof(UserService));
7374
}
7475

7576
public async Task<MembershipRequest> AddMembershipRequestAsync(Organization organization, string memberName, bool isAdmin)
@@ -598,19 +599,19 @@ private async Task SubscribeOrganizationToTenantPolicyIfTenantIdIsSupported(User
598599
var tenantId = adminUser.Credentials.GetAzureActiveDirectoryCredential()?.TenantId;
599600
if (string.IsNullOrEmpty(tenantId))
600601
{
601-
Logger.LogInformation("Will not apply tenant policy to organization because admin user does not have an AAD credential.");
602+
DiagnosticsSource.LogInformation("Will not apply tenant policy to organization because admin user does not have an AAD credential.");
602603
return;
603604
}
604605

605606
if (!ContentObjectService.LoginDiscontinuationConfiguration.IsTenantIdPolicySupportedForOrganization(
606607
organization.EmailAddress ?? organization.UnconfirmedEmailAddress,
607608
tenantId))
608609
{
609-
Logger.LogInformation("Will not apply tenant policy to organization because policy is not supported for email-tenant pair.");
610+
DiagnosticsSource.LogInformation("Will not apply tenant policy to organization because policy is not supported for email-tenant pair.");
610611
return;
611612
}
612613

613-
Logger.LogInformation("Applying tenant policy to organization.");
614+
DiagnosticsSource.LogInformation("Applying tenant policy to organization.");
614615
var tenantPolicy = RequireOrganizationTenantPolicy.Create(tenantId);
615616
await SecurityPolicyService.SubscribeAsync(organization, tenantPolicy, commitChanges);
616617
}

tests/NuGetGallery.Facts/TestUtils/TestServiceUtility.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Linq.Expressions;
8-
using Microsoft.Extensions.Logging;
98
using Moq;
109
using NuGet.Services.Entities;
1110
using NuGetGallery.Configuration;
11+
using NuGetGallery.Diagnostics;
1212
using NuGetGallery.Framework;
1313
using NuGetGallery.Security;
1414

@@ -26,7 +26,7 @@ public class TestableUserService : UserService
2626
public Mock<IContentObjectService> MockConfigObjectService { get; protected set; }
2727
public Mock<IDateTimeProvider> MockDateTimeProvider { get; protected set; }
2828
public Mock<ITelemetryService> MockTelemetryService { get; protected set; }
29-
public Mock<ILogger<TestableUserService>> MockLogger { get; protected set; }
29+
public Mock<IDiagnosticsSource> MockDiagnosticsSource { get; protected set; }
3030

3131
public TestableUserService()
3232
{
@@ -40,7 +40,7 @@ public TestableUserService()
4040
DateTimeProvider = (MockDateTimeProvider = new Mock<IDateTimeProvider>()).Object;
4141
Auditing = new TestAuditingService();
4242
TelemetryService = (MockTelemetryService = new Mock<ITelemetryService>()).Object;
43-
Logger = (MockLogger = new Mock<ILogger<TestableUserService>>()).Object;
43+
DiagnosticsSource = (MockDiagnosticsSource = new Mock<IDiagnosticsSource>()).Object;
4444

4545
// Set ConfirmEmailAddress to a default of true
4646
MockConfig.Setup(c => c.ConfirmEmailAddresses).Returns(true);

0 commit comments

Comments
 (0)