Skip to content

Commit a8a3061

Browse files
authored
Convert SC primary instance loggers to ILogger (#5019)
* Convert SC primary instance loggers to ILogger * Register EmailSender in DI container since dependents now expect it to be injected * Use CreateStaticLogger for command loggers * Logging parameters converted to Pascal case * Remove unnecessary fields
1 parent 4f25d49 commit a8a3061

File tree

75 files changed

+566
-542
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+566
-542
lines changed

src/Particular.LicensingComponent/AuditThroughput/AuditThroughputCollectorHostedService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class AuditThroughputCollectorHostedService(
2020

2121
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
2222
{
23-
logger.LogInformation($"Starting {nameof(AuditThroughputCollectorHostedService)}");
23+
logger.LogInformation("Starting {ServiceName}", nameof(AuditThroughputCollectorHostedService));
2424

2525
try
2626
{
@@ -42,14 +42,14 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
4242
}
4343
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
4444
{
45-
logger.LogInformation($"Stopping {nameof(AuditThroughputCollectorHostedService)}");
45+
logger.LogInformation("Stopping {ServiceName}", nameof(AuditThroughputCollectorHostedService));
4646
}
4747
}
4848

4949
async Task GatherThroughput(CancellationToken cancellationToken)
5050
{
5151
var utcYesterday = DateOnly.FromDateTime(timeProvider.GetUtcNow().DateTime).AddDays(-1);
52-
logger.LogInformation($"Gathering throughput from audit for {utcYesterday.ToShortDateString()}");
52+
logger.LogInformation("Gathering throughput from audit for {AuditDate}", utcYesterday.ToShortDateString());
5353

5454
await VerifyAuditInstances(cancellationToken);
5555

@@ -115,18 +115,18 @@ async Task VerifyAuditInstances(CancellationToken cancellationToken)
115115
{
116116
if (remote.Status == "online" || remote.SemanticVersion is not null)
117117
{
118-
logger.LogInformation($"ServiceControl Audit instance at {remote.ApiUri} detected running version {remote.SemanticVersion}");
118+
logger.LogInformation("ServiceControl Audit instance at {RemoteApiUri} detected running version {RemoteSemanticVersion}", remote.ApiUri, remote.SemanticVersion);
119119
}
120120
else
121121
{
122-
logger.LogWarning($"Unable to determine the version of one or more ServiceControl Audit instances. For the instance with URI {remote.ApiUri}, the status was '{remote.Status}' and the version string returned was '{remote.VersionString}'.");
122+
logger.LogWarning("Unable to determine the version of one or more ServiceControl Audit instances. For the instance with URI {RemoteApiUri}, the status was '{RemoteStatus}' and the version string returned was '{RemoteVersionString}'.", remote.ApiUri, remote.Status, remote.VersionString);
123123
}
124124
}
125125

126126
var allHaveAuditCounts = remotesInfo.All(auditQuery.ValidRemoteInstances);
127127
if (!allHaveAuditCounts)
128128
{
129-
logger.LogWarning($"At least one ServiceControl Audit instance is either not running the required version ({auditQuery.MinAuditCountsVersion}) or is not configured for at least 2 days of retention. Audit throughput will not be available.");
129+
logger.LogWarning("At least one ServiceControl Audit instance is either not running the required version ({RequiredAuditVersion}) or is not configured for at least 2 days of retention. Audit throughput will not be available.", auditQuery.MinAuditCountsVersion);
130130
}
131131
}
132132

src/Particular.LicensingComponent/BrokerThroughput/BrokerThroughputCollectorHostedService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ static ReadOnlyDictionary<string, string> LoadBrokerSettingValues(IEnumerable<Ke
2929

3030
if (brokerThroughputQuery.HasInitialisationErrors(out var errorMessage))
3131
{
32-
logger.LogError($"Could not start {nameof(BrokerThroughputCollectorHostedService)}, due to initialisation errors:\n{errorMessage}");
32+
logger.LogError("Could not start {ServiceName}, due to initialisation errors:\n{InitializationErrors}", nameof(BrokerThroughputCollectorHostedService), errorMessage);
3333
return;
3434
}
3535

36-
logger.LogInformation($"Starting {nameof(BrokerThroughputCollectorHostedService)}");
36+
logger.LogInformation("Starting {ServiceName}", nameof(BrokerThroughputCollectorHostedService));
3737

3838
try
3939
{
@@ -55,7 +55,7 @@ static ReadOnlyDictionary<string, string> LoadBrokerSettingValues(IEnumerable<Ke
5555
}
5656
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
5757
{
58-
logger.LogInformation($"Stopping {nameof(BrokerThroughputCollectorHostedService)}");
58+
logger.LogInformation("Stopping {ServiceName}", nameof(BrokerThroughputCollectorHostedService));
5959
}
6060
}
6161

src/Particular.LicensingComponent/MonitoringThroughput/MonitoringThroughputHostedService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ async Task Handle(MessageContext message, CancellationToken cancellationToken)
2424

2525
public async Task StartAsync(CancellationToken cancellationToken)
2626
{
27-
logger.LogInformation($"Starting {nameof(MonitoringThroughputHostedService)}");
27+
logger.LogInformation("Starting {ServiceName}", nameof(MonitoringThroughputHostedService));
2828

2929
transportInfrastructure = await transportCustomization.CreateTransportInfrastructure(ServiceControlSettings.ServiceControlThroughputDataQueue, transportSettings, Handle, (_, __) => Task.FromResult(ErrorHandleResult.Handled), (_, __) => Task.CompletedTask);
3030
await transportInfrastructure.Receivers[ServiceControlSettings.ServiceControlThroughputDataQueue].StartReceive(cancellationToken);
3131
}
3232

3333
public async Task StopAsync(CancellationToken cancellationToken)
3434
{
35-
logger.LogInformation($"Stopping {nameof(MonitoringThroughputHostedService)}");
35+
logger.LogInformation("Stopping {ServiceName}", nameof(MonitoringThroughputHostedService));
3636

3737
if (transportInfrastructure != null)
3838
{

src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Hosting.Commands;
77
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging.Abstractions;
89
using NServiceBus;
910
using NUnit.Framework;
1011
using Particular.ServiceControl.Hosting;
@@ -57,7 +58,7 @@ public async Task CanRunMaintenanceMode()
5758
public async Task CanRunImportFailedMessagesMode()
5859
=> await new TestableImportFailedErrorsCommand().Execute(new HostArguments(Array.Empty<string>()), settings);
5960

60-
class TestableImportFailedErrorsCommand : ImportFailedErrorsCommand
61+
class TestableImportFailedErrorsCommand() : ImportFailedErrorsCommand()
6162
{
6263
protected override EndpointConfiguration CreateEndpointConfiguration(Settings settings)
6364
{

src/ServiceControl.AcceptanceTests/Recoverability/MessageFailures/When_a_retry_fails_to_be_sent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using AcceptanceTesting.EndpointTemplates;
99
using Infrastructure;
1010
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging.Abstractions;
1112
using NServiceBus;
1213
using NServiceBus.AcceptanceTesting;
1314
using NServiceBus.Routing;
@@ -146,7 +147,7 @@ public class MyContext : ScenarioContext
146147
public class MessageThatWillFail : ICommand;
147148

148149
public class FakeReturnToSender(IErrorMessageDataStore errorMessageStore, MyContext myContext)
149-
: ReturnToSender(errorMessageStore)
150+
: ReturnToSender(errorMessageStore, NullLogger<ReturnToSender>.Instance)
150151
{
151152
public override Task HandleMessage(MessageContext message, IMessageDispatcher sender, string errorQueueTransportAddress, CancellationToken cancellationToken = default)
152153
{

src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.AspNetCore.TestHost;
1616
using Microsoft.Extensions.DependencyInjection;
1717
using Microsoft.Extensions.Hosting;
18+
using Microsoft.Extensions.Logging.Abstractions;
1819
using NLog;
1920
using NServiceBus;
2021
using NServiceBus.AcceptanceTesting;

src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
using Microsoft.Extensions.Hosting;
99
using Microsoft.Extensions.Logging;
1010
using NServiceBus;
11+
using ServiceControl.Infrastructure;
1112
using Settings;
1213

13-
class ImportFailedAuditsCommand(ILogger<ImportFailedAuditsCommand> logger) : AbstractCommand
14+
class ImportFailedAuditsCommand : AbstractCommand
1415
{
1516
public override async Task Execute(HostArguments args, Settings settings)
1617
{
@@ -40,7 +41,7 @@ public override async Task Execute(HostArguments args, Settings settings)
4041
}
4142
catch (OperationCanceledException e) when (tokenSource.IsCancellationRequested)
4243
{
43-
logger.LogInformation(e, "Cancelled");
44+
LoggerUtil.CreateStaticLogger<ImportFailedAuditsCommand>().LogInformation(e, "Cancelled");
4445
}
4546
finally
4647
{

src/ServiceControl.Audit/Infrastructure/Hosting/Commands/SetupCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Settings;
1010
using Transports;
1111

12-
class SetupCommand() : AbstractCommand
12+
class SetupCommand : AbstractCommand
1313
{
1414
public override async Task Execute(HostArguments args, Settings settings)
1515
{

src/ServiceControl.Persistence.Tests/MonitoringDataStoreTests.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging.Abstractions;
67
using NUnit.Framework;
78
using ServiceControl.Monitoring;
89
using ServiceControl.Operations;
@@ -13,7 +14,7 @@ class MonitoringDataStoreTests : PersistenceTestBase
1314
[Test]
1415
public async Task Endpoints_load_from_dataStore_into_monitor()
1516
{
16-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
17+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
1718
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
1819
await MonitoringDataStore.CreateIfNotExists(endpoint1);
1920

@@ -26,7 +27,7 @@ public async Task Endpoints_load_from_dataStore_into_monitor()
2627
[Test]
2728
public async Task Endpoints_added_more_than_once_are_treated_as_same_endpoint()
2829
{
29-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
30+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
3031
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
3132
await MonitoringDataStore.CreateIfNotExists(endpoint1);
3233
await MonitoringDataStore.CreateIfNotExists(endpoint1);
@@ -40,7 +41,7 @@ public async Task Endpoints_added_more_than_once_are_treated_as_same_endpoint()
4041
[Test]
4142
public async Task Updating_existing_endpoint_does_not_create_new_ones()
4243
{
43-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
44+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
4445
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
4546
await MonitoringDataStore.CreateIfNotExists(endpoint1);
4647
await MonitoringDataStore.CreateOrUpdate(endpoint1, endpointInstanceMonitoring);
@@ -54,7 +55,7 @@ public async Task Updating_existing_endpoint_does_not_create_new_ones()
5455
[Test]
5556
public async Task Endpoint_is_created_if_doesnt_exist()
5657
{
57-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
58+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
5859
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
5960
var endpoint2 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host2", Name = "Name2" };
6061
await MonitoringDataStore.CreateIfNotExists(endpoint1);
@@ -69,7 +70,7 @@ public async Task Endpoint_is_created_if_doesnt_exist()
6970
[Test]
7071
public async Task Endpoint_is_created_if_doesnt_exist_on_update()
7172
{
72-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
73+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
7374
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
7475
var endpoint2 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host2", Name = "Name2" };
7576
await MonitoringDataStore.CreateIfNotExists(endpoint1);
@@ -84,7 +85,7 @@ public async Task Endpoint_is_created_if_doesnt_exist_on_update()
8485
[Test]
8586
public async Task Endpoint_is_updated()
8687
{
87-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
88+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
8889
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
8990
await MonitoringDataStore.CreateIfNotExists(endpoint1);
9091

@@ -93,7 +94,7 @@ public async Task Endpoint_is_updated()
9394
Assert.That(endpointInstanceMonitoring.IsMonitored(endpointInstanceMonitoring.GetEndpoints()[0].Id), Is.False);
9495

9596
await MonitoringDataStore.UpdateEndpointMonitoring(endpoint1, true);
96-
endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
97+
endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
9798

9899
CompleteDatabaseOperation();
99100
await MonitoringDataStore.WarmupMonitoringFromPersistence(endpointInstanceMonitoring);
@@ -104,7 +105,7 @@ public async Task Endpoint_is_updated()
104105
[Test]
105106
public async Task Endpoint_is_deleted()
106107
{
107-
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
108+
var endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
108109
var endpoint1 = new EndpointDetails() { HostId = Guid.NewGuid(), Host = "Host1", Name = "Name1" };
109110
await MonitoringDataStore.CreateIfNotExists(endpoint1);
110111

@@ -114,7 +115,7 @@ public async Task Endpoint_is_deleted()
114115

115116
await MonitoringDataStore.Delete(endpointInstanceMonitoring.GetEndpoints()[0].Id);
116117

117-
endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents());
118+
endpointInstanceMonitoring = new EndpointInstanceMonitoring(new FakeDomainEvents(), NullLogger<EndpointInstanceMonitor>.Instance);
118119

119120
CompleteDatabaseOperation();
120121
await MonitoringDataStore.WarmupMonitoringFromPersistence(endpointInstanceMonitoring);

src/ServiceControl.Persistence.Tests/Recoverability/ReturnToSenderDequeuerTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using MessageFailures;
1111
using MessageFailures.Api;
1212
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.Logging.Abstractions;
1314
using NServiceBus.Extensibility;
1415
using NServiceBus.Transport;
1516
using NUnit.Framework;
@@ -47,7 +48,7 @@ public async Task It_removes_staging_id_header()
4748
};
4849
var message = CreateMessage(Guid.NewGuid().ToString(), headers);
4950

50-
await new ReturnToSender(null).HandleMessage(message, sender, "error");
51+
await new ReturnToSender(null, NullLogger<ReturnToSender>.Instance).HandleMessage(message, sender, "error");
5152

5253
Assert.That(sender.Message.Headers.ContainsKey("ServiceControl.Retry.StagingId"), Is.False);
5354
}
@@ -66,7 +67,7 @@ public async Task It_fetches_the_body_from_storage_if_provided()
6667
};
6768
var message = CreateMessage(Guid.NewGuid().ToString(), headers);
6869

69-
await new ReturnToSender(new FakeErrorMessageDataStore()).HandleMessage(message, sender, "error");
70+
await new ReturnToSender(new FakeErrorMessageDataStore(), NullLogger<ReturnToSender>.Instance).HandleMessage(message, sender, "error");
7071

7172
Assert.That(Encoding.UTF8.GetString(sender.Message.Body.ToArray()), Is.EqualTo("MessageBodyId"));
7273
}
@@ -84,7 +85,7 @@ public async Task It_uses_retry_to_if_provided()
8485
};
8586
var message = CreateMessage(Guid.NewGuid().ToString(), headers);
8687

87-
await new ReturnToSender(null).HandleMessage(message, sender, "error");
88+
await new ReturnToSender(null, NullLogger<ReturnToSender>.Instance).HandleMessage(message, sender, "error");
8889

8990
Assert.Multiple(() =>
9091
{
@@ -105,7 +106,7 @@ public async Task It_sends_directly_to_target_if_retry_to_is_not_provided()
105106
};
106107
var message = CreateMessage(Guid.NewGuid().ToString(), headers);
107108

108-
await new ReturnToSender(null).HandleMessage(message, sender, "error");
109+
await new ReturnToSender(null, NullLogger<ReturnToSender>.Instance).HandleMessage(message, sender, "error");
109110

110111
Assert.Multiple(() =>
111112
{
@@ -129,7 +130,7 @@ public async Task It_restores_body_id_and_target_addres_after_failure()
129130

130131
try
131132
{
132-
await new ReturnToSender(null).HandleMessage(message, sender, "error");
133+
await new ReturnToSender(null, NullLogger<ReturnToSender>.Instance).HandleMessage(message, sender, "error");
133134
}
134135
catch (Exception)
135136
{

0 commit comments

Comments
 (0)