Skip to content

Commit 6df6fd2

Browse files
committed
cleanup remaining references to LogManager
1 parent 316516e commit 6df6fd2

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ async Task InitializeServiceControl(ScenarioContext context)
5050
var logPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
5151
Directory.CreateDirectory(logPath);
5252
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace, defaultLevel: LogLevel.Debug, logPath: logPath);
53+
LoggerUtil.ActiveLoggers = Loggers.Test;
5354

5455
var settings = new Settings(transportToUse.TypeName, persistenceToUse.PersistenceType, loggingSettings, forwardErrorMessages: false, errorRetentionPeriod: TimeSpan.FromDays(10))
5556
{
@@ -65,9 +66,9 @@ async Task InitializeServiceControl(ScenarioContext context)
6566
{
6667
var headers = messageContext.Headers;
6768
var id = messageContext.NativeMessageId;
68-
var log = NServiceBus.Logging.LogManager.GetLogger<ServiceControlComponentRunner>();
69+
var logger = LoggerUtil.CreateStaticLogger<ServiceControlComponentRunner>(loggingSettings.LogLevel);
6970
headers.TryGetValue(Headers.MessageId, out var originalMessageId);
70-
log.Debug($"OnMessage for message '{id}'({originalMessageId ?? string.Empty}).");
71+
logger.LogDebug("OnMessage for message '{MessageId}'({OriginalMessageId})", id, originalMessageId ?? string.Empty);
7172

7273
//Do not filter out CC, SA and HB messages as they can't be stamped
7374
if (headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes)
@@ -86,7 +87,7 @@ async Task InitializeServiceControl(ScenarioContext context)
8687
var currentSession = context.TestRunId.ToString();
8788
if (!headers.TryGetValue("SC.SessionID", out var session) || session != currentSession)
8889
{
89-
log.Debug($"Discarding message '{id}'({originalMessageId ?? string.Empty}) because it's session id is '{session}' instead of '{currentSession}'.");
90+
logger.LogDebug("Discarding message '{MessageId}'({OriginalMessageId}) because it's session id is '{SessionId}' instead of '{CurrentSessionId}'", id, originalMessageId ?? string.Empty, session, currentSession);
9091
return true;
9192
}
9293

src/ServiceControl.Audit.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async Task InitializeServiceControl(ScenarioContext context)
4545
Directory.CreateDirectory(logPath);
4646

4747
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace, defaultLevel: LogLevel.Debug, logPath: logPath);
48+
LoggerUtil.ActiveLoggers = Loggers.Test;
4849

4950
settings = new Settings(transportToUse.TypeName, persistenceToUse.PersistenceType, loggingSettings)
5051
{
@@ -56,9 +57,9 @@ async Task InitializeServiceControl(ScenarioContext context)
5657
{
5758
var id = messageContext.NativeMessageId;
5859
var headers = messageContext.Headers;
59-
var log = LoggerUtil.CreateStaticLogger<ServiceControlComponentRunner>();
60+
var logger = LoggerUtil.CreateStaticLogger<ServiceControlComponentRunner>(loggingSettings.LogLevel);
6061
headers.TryGetValue(Headers.MessageId, out var originalMessageId);
61-
log.LogDebug("OnMessage for message '{MessageId}'({OriginalMessageId}).", id, originalMessageId ?? string.Empty);
62+
logger.LogDebug("OnMessage for message '{MessageId}'({OriginalMessageId}).", id, originalMessageId ?? string.Empty);
6263

6364
//Do not filter out CC, SA and HB messages as they can't be stamped
6465
if (headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes)
@@ -77,7 +78,7 @@ async Task InitializeServiceControl(ScenarioContext context)
7778
var currentSession = context.TestRunId.ToString();
7879
if (!headers.TryGetValue("SC.SessionID", out var session) || session != currentSession)
7980
{
80-
log.LogDebug("Discarding message '{MessageId}'({OriginalMessageId}) because it's session id is '{SessionId}' instead of '{CurrentSessionId}'.", id, originalMessageId ?? string.Empty, session, currentSession);
81+
logger.LogDebug("Discarding message '{MessageId}'({OriginalMessageId}) because it's session id is '{SessionId}' instead of '{CurrentSessionId}'.", id, originalMessageId ?? string.Empty, session, currentSession);
8182
return true;
8283
}
8384

src/ServiceControl.Infrastructure/LoggingConfigurator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ namespace ServiceControl.Infrastructure
1212
using LogManager = NServiceBus.Logging.LogManager;
1313
using LogLevel = NLog.LogLevel;
1414

15-
// TODO: Migrate from NLog to .NET logging
1615
public static class LoggingConfigurator
1716
{
1817
public static void ConfigureLogging(LoggingSettings loggingSettings)
1918
{
20-
if (NLog.LogManager.Configuration != null)
19+
//used for loggers outside of ServiceControl (i.e. transports and core) to use the logger factory defined here
20+
LogManager.UseFactory(new ExtensionsLoggerFactory(LoggerFactory.Create(configure => configure.BuildLogger(loggingSettings.LogLevel))));
21+
22+
if (!LoggerUtil.IsLoggingTo(Loggers.NLog) || NLog.LogManager.Configuration != null)
2123
{
2224
return;
2325
}
2426

27+
//configure NLog
2528
var nlogConfig = new LoggingConfiguration();
2629
var simpleLayout = new SimpleLayout("${longdate}|${processtime}|${threadid}|${level}|${logger}|${message}${onexception:|${exception:format=tostring}}");
2730

@@ -76,8 +79,7 @@ public static void ConfigureLogging(LoggingSettings loggingSettings)
7679

7780
NLog.LogManager.Configuration = nlogConfig;
7881

79-
LogManager.UseFactory(new ExtensionsLoggerFactory(LoggerFactory.Create(configure => configure.BuildLogger(loggingSettings.LogLevel))));
80-
82+
//using LogManager here rather than LoggerUtil.CreateStaticLogger since this is exclusive to NLog
8183
var logger = LogManager.GetLogger("LoggingConfiguration");
8284
var logEventInfo = new LogEventInfo { TimeStamp = DateTime.UtcNow };
8385
var loggingTo = AppEnvironment.RunningInContainer ? "console" : fileTarget.FileName.Render(logEventInfo);

src/ServiceControl.Infrastructure/RavenDbLogLevelToLogsModeMapper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
namespace ServiceControl
22
{
33
using Microsoft.Extensions.Logging;
4-
using NServiceBus.Logging;
54

65
public class RavenDbLogLevelToLogsModeMapper
76
{
8-
static readonly ILog Logger = LogManager.GetLogger(typeof(RavenDbLogLevelToLogsModeMapper));
9-
107
public static string Map(string ravenDbLogLevel, ILogger logger)
118
{
129
switch (ravenDbLogLevel.ToLower())
@@ -25,7 +22,7 @@ public static string Map(string ravenDbLogLevel, ILogger logger)
2522
case "operations":
2623
return "Operations";
2724
default:
28-
Logger.WarnFormat("Unknown log level '{0}', mapped to 'Operations'", ravenDbLogLevel);
25+
logger.LogWarning("Unknown log level '{RavenDbLogLevel}', mapped to 'Operations'", ravenDbLogLevel);
2926
return "Operations";
3027
}
3128
}

src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ namespace ServiceControl.Monitoring.AcceptanceTests.TestSupport
1313
using Microsoft.AspNetCore.TestHost;
1414
using Microsoft.Extensions.DependencyInjection;
1515
using Microsoft.Extensions.Hosting;
16+
using Microsoft.Extensions.Logging;
1617
using Monitoring;
1718
using NServiceBus;
1819
using NServiceBus.AcceptanceTesting;
1920
using NServiceBus.AcceptanceTesting.Support;
20-
using NServiceBus.Logging;
21+
using ServiceControl.Infrastructure;
2122

2223
class ServiceControlComponentRunner(
2324
ITransportIntegration transportToUse,
@@ -33,15 +34,16 @@ class ServiceControlComponentRunner(
3334

3435
async Task InitializeServiceControl(ScenarioContext context)
3536
{
37+
LoggerUtil.ActiveLoggers = Loggers.Test;
3638
settings = new Settings(transportType: transportToUse.TypeName)
3739
{
3840
ConnectionString = transportToUse.ConnectionString,
3941
HttpHostName = "localhost",
4042
OnMessage = (id, headers, body, @continue) =>
4143
{
42-
var log = LogManager.GetLogger<ServiceControlComponentRunner>();
44+
var logger = LoggerUtil.CreateStaticLogger<ServiceControlComponentRunner>();
4345
headers.TryGetValue(Headers.MessageId, out var originalMessageId);
44-
log.Debug($"OnMessage for message '{id}'({originalMessageId ?? string.Empty}).");
46+
logger.LogDebug("OnMessage for message '{MessageId}'({OriginalMessageId})", id, originalMessageId ?? string.Empty);
4547

4648
//Do not filter out CC, SA and HB messages as they can't be stamped
4749
if (headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes)
@@ -60,7 +62,7 @@ async Task InitializeServiceControl(ScenarioContext context)
6062
var currentSession = context.TestRunId.ToString();
6163
if (!headers.TryGetValue("SC.SessionID", out var session) || session != currentSession)
6264
{
63-
log.Debug($"Discarding message '{id}'({originalMessageId ?? string.Empty}) because it's session id is '{session}' instead of '{currentSession}'.");
65+
logger.LogDebug("Discarding message '{MessageId}'({OriginalMessageId}) because it's session id is '{SessionId}' instead of '{CurrentSessionId}'", id, originalMessageId ?? string.Empty, session, currentSession);
6466
return Task.CompletedTask;
6567
}
6668

@@ -91,12 +93,15 @@ async Task InitializeServiceControl(ScenarioContext context)
9193
// Force the DI container to run the dependency resolution check to verify all dependencies can be resolved
9294
EnvironmentName = Environments.Development
9395
});
96+
hostBuilder.Logging.ClearProviders();
97+
hostBuilder.Logging.BuildLogger(LogLevel.Information);
98+
9499
hostBuilder.AddServiceControlMonitoring((criticalErrorContext, cancellationToken) =>
95100
{
96101
var logitem = new ScenarioContext.LogItem
97102
{
98103
Endpoint = settings.InstanceName,
99-
Level = LogLevel.Fatal,
104+
Level = NServiceBus.Logging.LogLevel.Fatal,
100105
LoggerName = $"{settings.InstanceName}.CriticalError",
101106
Message = $"{criticalErrorContext.Error}{Environment.NewLine}{criticalErrorContext.Exception}"
102107
};

src/ServiceControl.Persistence/ServiceControl.Persistence.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<ProjectReference Include="..\ServiceControl.Configuration\ServiceControl.Configuration.csproj" />
99
<ProjectReference Include="..\ServiceControl.DomainEvents\ServiceControl.DomainEvents.csproj" />
10+
<ProjectReference Include="..\ServiceControl.Infrastructure\ServiceControl.Infrastructure.csproj" />
1011
<ProjectReference Include="..\ServiceControl.SagaAudit\ServiceControl.SagaAudit.csproj" />
1112
</ItemGroup>
1213

src/ServiceControl/HostApplicationBuilderExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace Particular.ServiceControl
2121
using Microsoft.Extensions.Hosting;
2222
using Microsoft.Extensions.Hosting.WindowsServices;
2323
using Microsoft.Extensions.Logging;
24-
using NLog.Extensions.Logging;
2524
using NServiceBus;
2625
using NServiceBus.Configuration.AdvancedExtensibility;
2726
using NServiceBus.Transport;
@@ -42,11 +41,8 @@ public static void AddServiceControl(this IHostApplicationBuilder hostBuilder, S
4241
EventSourceCreator.Create();
4342
}
4443

45-
var logging = hostBuilder.Logging;
46-
logging.ClearProviders();
47-
//HINT: configuration used by NLog comes from LoggingConfigurator.cs
48-
logging.AddNLog();
49-
logging.SetMinimumLevel(settings.LoggingSettings.LogLevel);
44+
hostBuilder.Logging.ClearProviders();
45+
hostBuilder.Logging.BuildLogger(settings.LoggingSettings.LogLevel);
5046

5147
var services = hostBuilder.Services;
5248
var transportSettings = settings.ToTransportSettings();

0 commit comments

Comments
 (0)