Skip to content

Commit a9007b4

Browse files
Switch to .NET logging
1 parent d002ada commit a9007b4

File tree

15 files changed

+118
-106
lines changed

15 files changed

+118
-106
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckDirtyMemory.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@ namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks;
33
using System;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging;
67
using NServiceBus.CustomChecks;
7-
using NServiceBus.Logging;
88

9-
class CheckDirtyMemory(MemoryInformationRetriever memoryInformationRetriever) : CustomCheck("RavenDB dirty memory", "ServiceControl.Audit Health", TimeSpan.FromMinutes(5))
9+
class CheckDirtyMemory(MemoryInformationRetriever memoryInformationRetriever, ILogger<CheckDirtyMemory> logger) : CustomCheck("RavenDB dirty memory", "ServiceControl.Audit Health", TimeSpan.FromMinutes(5))
1010
{
11+
1112
public override async Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1213
{
1314
var (isHighDirty, dirtyMemory) = await memoryInformationRetriever.GetMemoryInformation(cancellationToken);
1415

15-
Log.Debug($"RavenDB dirty memory value: {dirtyMemory}.");
16+
logger.LogDebug("RavenDB dirty memory value: {DirtyMemory}.", dirtyMemory);
1617

1718
if (isHighDirty)
1819
{
19-
var message = $"There is a high level of RavenDB dirty memory ({dirtyMemory}). See https://docs.particular.net/servicecontrol/troubleshooting#ravendb-dirty-memory for guidance on how to mitigate the issue.";
20-
Log.Warn(message);
21-
return CheckResult.Failed(message);
20+
logger.LogWarning("There is a high level of RavenDB dirty memory ({DirtyMemory}). See https://docs.particular.net/servicecontrol/troubleshooting#ravendb-dirty-memory for guidance on how to mitigate the issue.", dirtyMemory);
21+
return CheckResult.Failed($"There is a high level of RavenDB dirty memory ({dirtyMemory}). See https://docs.particular.net/servicecontrol/troubleshooting#ravendb-dirty-memory for guidance on how to mitigate the issue.");
2222
}
2323

2424
return CheckResult.Pass;
2525
}
26-
27-
static readonly ILog Log = LogManager.GetLogger<CheckDirtyMemory>();
2826
}

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckFreeDiskSpace.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
using System.IO;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging;
89
using NServiceBus.CustomChecks;
9-
using NServiceBus.Logging;
1010
using RavenDB;
11+
using ServiceControl.Infrastructure;
1112

12-
class CheckFreeDiskSpace(DatabaseConfiguration databaseConfiguration) : CustomCheck("ServiceControl.Audit database", "Storage space", TimeSpan.FromMinutes(5))
13+
class CheckFreeDiskSpace(DatabaseConfiguration databaseConfiguration, ILogger<CheckFreeDiskSpace> logger) : CustomCheck("ServiceControl.Audit database", "Storage space", TimeSpan.FromMinutes(5))
1314
{
1415
public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1516
{
16-
if (Logger.IsDebugEnabled)
17+
if (logger.IsEnabled(LogLevel.Debug))
1718
{
18-
Logger.Debug($"Check ServiceControl data drive space remaining custom check starting. Threshold {percentageThreshold:P0}");
19+
logger.LogDebug("Check ServiceControl data drive space remaining custom check starting. Threshold {PercentageThreshold:P0}", percentageThreshold);
1920
}
2021

2122
if (!databaseConfiguration.ServerConfiguration.UseEmbeddedServer)
@@ -34,9 +35,9 @@ public override Task<CheckResult> PerformCheck(CancellationToken cancellationTok
3435

3536
var percentRemaining = (decimal)dataDriveInfo.AvailableFreeSpace / dataDriveInfo.TotalSize;
3637

37-
if (Logger.IsDebugEnabled)
38+
if (logger.IsEnabled(LogLevel.Debug))
3839
{
39-
Logger.Debug($"Free space: {availableFreeSpace:N0}B | Total: {totalSpace:N0}B | Percent remaining {percentRemaining:P1}");
40+
logger.LogDebug("Free space: {AvailableFreeSpace:N0}B | Total: {TotalSpace:N0}B | Percent remaining {PercentRemaining:P1}", availableFreeSpace, totalSpace, percentRemaining);
4041
}
4142

4243
return percentRemaining > percentageThreshold
@@ -51,35 +52,30 @@ public static int Parse(IDictionary<string, string> settings)
5152
thresholdValue = $"{DataSpaceRemainingThresholdDefault}";
5253
}
5354

54-
string message;
5555
if (!int.TryParse(thresholdValue, out var threshold))
5656
{
57-
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} must be an integer.";
58-
Logger.Fatal(message);
59-
throw new Exception(message);
57+
Logger.LogCritical("{RavenPersistenceConfigurationDataSpaceRemainingThresholdKey} must be an integer.", RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey);
58+
throw new Exception($"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} must be an integer.");
6059
}
6160

6261
if (threshold < 0)
6362
{
64-
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, minimum value is 0.";
65-
Logger.Fatal(message);
66-
throw new Exception(message);
63+
Logger.LogCritical("{RavenPersistenceConfigurationDataSpaceRemainingThresholdKey} is invalid, minimum value is 0.", RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey);
64+
throw new Exception($"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, minimum value is 0.");
6765
}
6866

6967
if (threshold > 100)
7068
{
71-
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, maximum value is 100.";
72-
Logger.Fatal(message);
73-
throw new Exception(message);
69+
Logger.LogCritical("{RavenPersistenceConfigurationDataSpaceRemainingThresholdKey} is invalid, maximum value is 100.", RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey);
70+
throw new Exception($"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, maximum value is 100.");
7471
}
7572

7673
return threshold;
7774
}
7875

7976
readonly string dataPathRoot = Path.GetPathRoot(databaseConfiguration.ServerConfiguration.DbPath);
8077
readonly decimal percentageThreshold = databaseConfiguration.DataSpaceRemainingThreshold / 100m;
81-
78+
static readonly ILogger<CheckFreeDiskSpace> Logger = LoggerUtil.CreateStaticLogger<CheckFreeDiskSpace>();
8279
public const int DataSpaceRemainingThresholdDefault = 20;
83-
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckFreeDiskSpace));
8480
}
8581
}

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckMinimumStorageRequiredForIngestion.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
using System.IO;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging;
89
using NServiceBus.CustomChecks;
9-
using NServiceBus.Logging;
1010
using RavenDB;
11+
using ServiceControl.Infrastructure;
1112

12-
class CheckMinimumStorageRequiredForIngestion(MinimumRequiredStorageState stateHolder, DatabaseConfiguration databaseConfiguration) : CustomCheck("Audit Message Ingestion Process", "ServiceControl.Audit Health", TimeSpan.FromSeconds(5))
13+
class CheckMinimumStorageRequiredForIngestion(MinimumRequiredStorageState stateHolder, DatabaseConfiguration databaseConfiguration, ILogger<CheckMinimumStorageRequiredForIngestion> logger) : CustomCheck("Audit Message Ingestion Process", "ServiceControl.Audit Health", TimeSpan.FromSeconds(5))
1314
{
1415
public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1516
{
1617
var percentageThreshold = databaseConfiguration.MinimumStorageLeftRequiredForIngestion / 100m;
1718

18-
if (Logger.IsDebugEnabled)
19+
if (logger.IsEnabled(LogLevel.Debug))
1920
{
20-
Logger.Debug($"Check ServiceControl data drive space starting. Threshold {percentageThreshold:P0}");
21+
logger.LogDebug("Check ServiceControl data drive space starting. Threshold {PercentageThreshold:P0}", percentageThreshold);
2122
}
2223

2324
// Should be checking UseEmbeddedServer but need to check DbPath instead for the ATT hack to work
@@ -35,9 +36,9 @@ public override Task<CheckResult> PerformCheck(CancellationToken cancellationTok
3536

3637
var percentRemaining = (decimal)dataDriveInfo.AvailableFreeSpace / dataDriveInfo.TotalSize;
3738

38-
if (Logger.IsDebugEnabled)
39+
if (logger.IsEnabled(LogLevel.Debug))
3940
{
40-
Logger.Debug($"Free space: {availableFreeSpace} | Total: {totalSpace} | Percent remaining {percentRemaining:P0}");
41+
logger.LogDebug("Free space: {AvailableFreeSpace} | Total: {TotalSpace} | Percent remaining {PercentRemaining:P0}", availableFreeSpace, totalSpace, percentRemaining);
4142
}
4243

4344
if (percentRemaining > percentageThreshold)
@@ -46,10 +47,9 @@ public override Task<CheckResult> PerformCheck(CancellationToken cancellationTok
4647
return SuccessResult;
4748
}
4849

49-
var message = $"Audit message ingestion stopped! {percentRemaining:P0} disk space remaining on data drive '{dataDriveInfo.VolumeLabel} ({dataDriveInfo.RootDirectory})' on '{Environment.MachineName}'. This is less than {percentageThreshold}% - the minimal required space configured. The threshold can be set using the {RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} configuration setting.";
50-
Logger.Warn(message);
50+
logger.LogWarning("Audit message ingestion stopped! {PercentRemaining:P0} disk space remaining on data drive '{DataDriveInfoVolumeLabel} ({DataDriveInfoRootDirectory})' on '{EnvironmentMachineName}'. This is less than {PercentageThreshold}% - the minimal required space configured. The threshold can be set using the {RavenPersistenceConfigurationMinimumStorageLeftRequiredForIngestionKey} configuration setting.", percentRemaining, dataDriveInfo.VolumeLabel, dataDriveInfo.RootDirectory, Environment.MachineName, percentageThreshold, RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey);
5151
stateHolder.CanIngestMore = false;
52-
return CheckResult.Failed(message);
52+
return CheckResult.Failed($"Audit message ingestion stopped! {percentRemaining:P0} disk space remaining on data drive '{dataDriveInfo.VolumeLabel} ({dataDriveInfo.RootDirectory})' on '{Environment.MachineName}'. This is less than {percentageThreshold}% - the minimal required space configured. The threshold can be set using the {RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} configuration setting.");
5353
}
5454

5555
public static int Parse(IDictionary<string, string> settings)
@@ -61,30 +61,27 @@ public static int Parse(IDictionary<string, string> settings)
6161

6262
if (!int.TryParse(thresholdValue, out var threshold))
6363
{
64-
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} must be an integer.";
65-
Logger.Fatal(message);
66-
throw new Exception(message);
64+
Logger.LogCritical("{RavenPersistenceConfigurationMinimumStorageLeftRequiredForIngestionKey} must be an integer.", RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey);
65+
throw new Exception($"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} must be an integer.");
6766
}
6867

6968
if (threshold < 0)
7069
{
71-
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0.";
72-
Logger.Fatal(message);
73-
throw new Exception(message);
70+
Logger.LogCritical("{RavenPersistenceConfigurationMinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0.", RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey);
71+
throw new Exception($"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0.");
7472
}
7573

7674
if (threshold > 100)
7775
{
78-
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100.";
79-
Logger.Fatal(message);
80-
throw new Exception(message);
76+
Logger.LogCritical("{RavenPersistenceConfigurationMinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100.", RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey);
77+
throw new Exception($"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100.");
8178
}
8279

8380
return threshold;
8481
}
8582

8683
public const int MinimumStorageLeftRequiredForIngestionDefault = 5;
8784
static readonly Task<CheckResult> SuccessResult = Task.FromResult(CheckResult.Pass);
88-
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckMinimumStorageRequiredForIngestion));
85+
static readonly ILogger<CheckMinimumStorageRequiredForIngestion> Logger = LoggerUtil.CreateStaticLogger<CheckMinimumStorageRequiredForIngestion>();
8986
}
9087
}

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckRavenDBIndexLag.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using System.Text;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging;
89
using NServiceBus.CustomChecks;
9-
using NServiceBus.Logging;
1010
using Raven.Client.Documents.Operations;
1111
using ServiceControl.Audit.Persistence.RavenDB;
12+
using ServiceControl.Infrastructure;
1213

13-
class CheckRavenDBIndexLag(IRavenDocumentStoreProvider documentStoreProvider) : CustomCheck("Audit Database Index Lag", "ServiceControl.Audit Health", TimeSpan.FromMinutes(5))
14+
class CheckRavenDBIndexLag(IRavenDocumentStoreProvider documentStoreProvider, ILogger<CheckRavenDBIndexLag> logger) : CustomCheck("Audit Database Index Lag", "ServiceControl.Audit Health", TimeSpan.FromMinutes(5))
1415
{
1516
public override async Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1617
{
@@ -20,7 +21,7 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
2021

2122
CreateDiagnosticsLogEntry(statistics, indexes);
2223

23-
var indexCountWithTooMuchLag = CheckAndReportIndexesWithTooMuchIndexLag(indexes);
24+
var indexCountWithTooMuchLag = CheckAndReportIndexesWithTooMuchIndexLag(indexes, logger);
2425

2526
if (indexCountWithTooMuchLag > 0)
2627
{
@@ -30,7 +31,7 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
3031
return CheckResult.Pass;
3132
}
3233

33-
static int CheckAndReportIndexesWithTooMuchIndexLag(IndexInformation[] indexes)
34+
static int CheckAndReportIndexesWithTooMuchIndexLag(IndexInformation[] indexes, ILogger logger)
3435
{
3536
int indexCountWithTooMuchLag = 0;
3637

@@ -43,12 +44,12 @@ static int CheckAndReportIndexesWithTooMuchIndexLag(IndexInformation[] indexes)
4344
if (indexLag > IndexLagThresholdError)
4445
{
4546
indexCountWithTooMuchLag++;
46-
Log.Error($"Index [{indexStats.Name}] IndexingLag {indexLag} is above error threshold ({IndexLagThresholdError}). Launch in maintenance mode to let indexes catch up.");
47+
logger.LogError("Index [{IndexStatsName}] IndexingLag {IndexLag} is above error threshold ({IndexLagThresholdError}). Launch in maintenance mode to let indexes catch up.", indexStats.Name, indexLag, IndexLagThresholdError);
4748
}
4849
else if (indexLag > IndexLagThresholdWarning)
4950
{
5051
indexCountWithTooMuchLag++;
51-
Log.Warn($"Index [{indexStats.Name}] IndexingLag {indexLag} is above warning threshold ({IndexLagThresholdWarning}). Launch in maintenance mode to let indexes catch up.");
52+
logger.LogWarning("Index [{IndexStatsName}] IndexingLag {IndexLag} is above warning threshold ({IndexLagThresholdWarning}). Launch in maintenance mode to let indexes catch up.", indexStats.Name, indexLag, IndexLagThresholdWarning);
5253
}
5354
}
5455
}
@@ -58,7 +59,7 @@ static int CheckAndReportIndexesWithTooMuchIndexLag(IndexInformation[] indexes)
5859

5960
static void CreateDiagnosticsLogEntry(DatabaseStatistics statistics, IndexInformation[] indexes)
6061
{
61-
if (!Log.IsDebugEnabled)
62+
if (!Logger.IsEnabled(LogLevel.Debug))
6263
{
6364
return;
6465
}
@@ -72,11 +73,12 @@ static void CreateDiagnosticsLogEntry(DatabaseStatistics statistics, IndexInform
7273
{
7374
report.AppendLine($"- Index [{indexStats.Name,-44}] State: {indexStats.State}, Stale: {indexStats.IsStale,-5}, Priority: {indexStats.Priority,-6}, LastIndexingTime: {indexStats.LastIndexingTime:u}");
7475
}
75-
Log.Debug(report.ToString());
76+
77+
Logger.LogDebug(report.ToString());
7678
}
7779

7880
static readonly TimeSpan IndexLagThresholdWarning = TimeSpan.FromMinutes(1);
7981
static readonly TimeSpan IndexLagThresholdError = TimeSpan.FromMinutes(10);
80-
static readonly ILog Log = LogManager.GetLogger<CheckRavenDBIndexLag>();
82+
static readonly ILogger<CheckRavenDBIndexLag> Logger = LoggerUtil.CreateStaticLogger<CheckRavenDBIndexLag>();
8183
}
8284
}

src/ServiceControl.Audit.Persistence.RavenDB/RavenEmbeddedPersistenceLifecycle.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace ServiceControl.Audit.Persistence.RavenDB
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.Extensions.Hosting;
9-
using NServiceBus.Logging;
9+
using Microsoft.Extensions.Logging;
1010
using Raven.Client.Documents;
1111
using Raven.Client.Exceptions.Database;
1212
using ServiceControl.RavenDB;
1313

14-
sealed class RavenEmbeddedPersistenceLifecycle(DatabaseConfiguration databaseConfiguration, IHostApplicationLifetime lifetime) : IRavenPersistenceLifecycle, IRavenDocumentStoreProvider, IDisposable
14+
sealed class RavenEmbeddedPersistenceLifecycle(DatabaseConfiguration databaseConfiguration, IHostApplicationLifetime lifetime, ILogger<RavenEmbeddedPersistenceLifecycle> logger) : IRavenPersistenceLifecycle, IRavenDocumentStoreProvider, IDisposable
1515
{
1616
public async ValueTask<IDocumentStore> GetDocumentStore(CancellationToken cancellationToken = default)
1717
{
@@ -61,7 +61,7 @@ public async Task Initialize(CancellationToken cancellationToken = default)
6161
}
6262
catch (DatabaseLoadTimeoutException e)
6363
{
64-
Log.Warn("Connecting to the embedded RavenDB database timed out. Retrying in 500ms...", e);
64+
logger.LogWarning(e, "Connecting to the embedded RavenDB database timed out. Retrying in 500ms...");
6565
await Task.Delay(500, cancellationToken);
6666
}
6767
}
@@ -83,7 +83,5 @@ public void Dispose()
8383
IDocumentStore? documentStore;
8484
EmbeddedDatabase? database;
8585
readonly SemaphoreSlim initializeSemaphore = new(1, 1);
86-
87-
static readonly ILog Log = LogManager.GetLogger(typeof(RavenEmbeddedPersistenceLifecycle));
8886
}
8987
}

0 commit comments

Comments
 (0)