Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a8c746f
convert SC transports from NServiceBus.Logging to Microsoft.Extension…
PhilBastian Jun 12, 2025
42d01e7
remove signedassembly requirement so that infrastructure can be imported
PhilBastian Jun 15, 2025
96ec124
revert previous change and instead propogate signing back to servicec…
PhilBastian Jun 15, 2025
5aaabe6
fix signature of customisation classes that are dynamically created
PhilBastian Jun 16, 2025
67920ff
add ilogger to test services and remove direct construction with logger
PhilBastian Jun 16, 2025
b3c690a
get tests to use ilogger
PhilBastian Jun 16, 2025
66aaa02
Switch to .NET logging
jasontaylordev Jun 10, 2025
e2196e6
Work in progress
jasontaylordev Jun 12, 2025
e9f287e
Remove test code
jasontaylordev Jun 13, 2025
9b97c92
Improve logging format for storage space details
jasontaylordev Jun 17, 2025
005968d
Properly shutdown NLog in Program.cs
jasontaylordev Jun 17, 2025
49ba86c
Remove Seq logging and prepare for .NET logging migration
jasontaylordev Jun 17, 2025
938c963
Update LogLevel format
jasontaylordev Jun 17, 2025
fb7f77e
Update LogLevel format in logging settings
jasontaylordev Jun 17, 2025
6c713bb
Merge branch 'master' into extensions.logging_transports
PhilBastian Jun 17, 2025
40eef99
Merge branch 'logging-spike-jt' into extensions.logging_transports
PhilBastian Jun 17, 2025
07d5606
enable adding test logging provider as part of loggerutils and create…
PhilBastian Jun 17, 2025
a843701
add ability to select logging provider from config
PhilBastian Jun 17, 2025
3e59886
handle setting not existing
PhilBastian Jun 17, 2025
ab580f6
change logmanager logger factory to the standard one now used by the …
PhilBastian Jun 17, 2025
3e16970
ensure logger for transport tests
PhilBastian Jun 18, 2025
c92f803
domain events and persister loggers
PhilBastian Jun 18, 2025
416258d
Merge branch 'extensions.logging_spike' into extensions.logging_remai…
PhilBastian Jun 18, 2025
479d409
set default to nlog and pass loglevel to test context
PhilBastian Jun 19, 2025
316516e
replace logmanager usage in persistence
PhilBastian Jun 19, 2025
6df6fd2
cleanup remaining references to LogManager
PhilBastian Jun 19, 2025
e37e282
missing files from last checkin
PhilBastian Jun 19, 2025
4485d7b
fix different dependency version test failure
PhilBastian Jun 20, 2025
4b80ba3
move loggerutil setup before first static logger usage
PhilBastian Jun 20, 2025
1200d13
Merge branch 'extensions.logging_spike' into extensions.logging_remai…
PhilBastian Jun 20, 2025
92b99b0
fix different dependency version test failure
PhilBastian Jun 20, 2025
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
2 changes: 2 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
<PackageVersion Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.10.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
Expand Down
12 changes: 9 additions & 3 deletions src/ServiceControl.AcceptanceTesting/DiscardMessagesBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ namespace ServiceControl.AcceptanceTesting
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.Logging;
using NServiceBus.Pipeline;
using ServiceControl.Infrastructure;

public class DiscardMessagesBehavior : IBehavior<ITransportReceiveContext, ITransportReceiveContext>
{
Expand Down Expand Up @@ -44,15 +45,20 @@ public Task Invoke(ITransportReceiveContext context, Func<ITransportReceiveConte
{
context.Message.Headers.TryGetValue(Headers.MessageId, out var originalMessageId);
context.Message.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var enclosedMessageTypes);
log.Debug($"Discarding message '{context.Message.MessageId}'({originalMessageId ?? string.Empty}) because it's session id is '{session}' instead of '{currentSession}' Message Types: {enclosedMessageTypes}.");
var logger = LoggerUtil.CreateStaticLogger<DiscardMessagesBehavior>();
logger.LogDebug("Discarding message '{MessageId}'({OriginalMessageId}) because it's session id is '{MessageSessionId}' instead of '{CurrentSessionId}' Message Types: {EnclosedMessageTypes}.",
context.Message.MessageId,
originalMessageId ?? string.Empty,
session,
currentSession,
enclosedMessageTypes);
return Task.CompletedTask;
}

return next(context);
}

ScenarioContext scenarioContext;
static ILog log = LogManager.GetLogger<DiscardMessagesBehavior>();

static string[] pluginMessages =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async Task InitializeServiceControl(ScenarioContext context)
var logPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(logPath);
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace, defaultLevel: LogLevel.Debug, logPath: logPath);
LoggerUtil.ActiveLoggers = Loggers.Test;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async Task InitializeServiceControl(ScenarioContext context)
Directory.CreateDirectory(logPath);

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal static DatabaseConfiguration GetDatabaseConfiguration(PersistenceSettin

if (settings.PersisterSpecificSettings.TryGetValue(RavenDbLogLevelKey, out var ravenDbLogLevel))
{
logsMode = RavenDbLogLevelToLogsModeMapper.Map(ravenDbLogLevel);
logsMode = RavenDbLogLevelToLogsModeMapper.Map(ravenDbLogLevel, Logger);
}

serverConfiguration = new ServerConfiguration(dbPath, serverUrl, logPath, logsMode);
Expand Down
13 changes: 4 additions & 9 deletions src/ServiceControl.DomainEvents/DomainEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus.Logging;
using Microsoft.Extensions.Logging;

public class DomainEvents : IDomainEvents
public class DomainEvents(IServiceProvider serviceProvider, ILogger<DomainEvents> logger) : IDomainEvents
{
static readonly ILog Log = LogManager.GetLogger<DomainEvents>();

readonly IServiceProvider serviceProvider;
public DomainEvents(IServiceProvider serviceProvider) => this.serviceProvider = serviceProvider;

public async Task Raise<T>(T domainEvent, CancellationToken cancellationToken) where T : IDomainEvent
{
var handlers = serviceProvider.GetServices<IDomainHandler<T>>();
Expand All @@ -25,7 +20,7 @@ await handler.Handle(domainEvent, cancellationToken)
}
catch (Exception e)
{
Log.Error($"Unexpected error publishing domain event {typeof(T)}", e);
logger.LogError(e, "Unexpected error publishing domain event {EventType}", typeof(T));
throw;
}
}
Expand All @@ -40,7 +35,7 @@ await handler.Handle(domainEvent, cancellationToken)
}
catch (Exception e)
{
Log.Error($"Unexpected error publishing domain event {typeof(T)}", e);
logger.LogError(e, "Unexpected error publishing domain event {EventType}", typeof(T));
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="NServiceBus" />
</ItemGroup>

Expand Down
13 changes: 9 additions & 4 deletions src/ServiceControl.Infrastructure/LoggerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ public static class LoggerUtil
{
public static Loggers ActiveLoggers { private get; set; } = Loggers.None;

public static bool IsLoggingTo(Loggers logger)
{
return (logger & ActiveLoggers) == logger;
}

public static void BuildLogger(this ILoggingBuilder loggingBuilder, LogLevel level)
{
if ((Loggers.Test & ActiveLoggers) == Loggers.Test)
if (IsLoggingTo(Loggers.Test))
{
loggingBuilder.Services.AddSingleton<ILoggerProvider>(new TestContextProvider());
loggingBuilder.Services.AddSingleton<ILoggerProvider>(new TestContextProvider(level));
}
if ((Loggers.NLog & ActiveLoggers) == Loggers.NLog)
if (IsLoggingTo(Loggers.NLog))
{
loggingBuilder.AddNLog();
}
if ((Loggers.Seq & ActiveLoggers) == Loggers.Seq)
if (IsLoggingTo(Loggers.Seq))
{
loggingBuilder.AddSeq();
}
Expand Down
10 changes: 6 additions & 4 deletions src/ServiceControl.Infrastructure/LoggingConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ namespace ServiceControl.Infrastructure
using LogManager = NServiceBus.Logging.LogManager;
using LogLevel = NLog.LogLevel;

// TODO: Migrate from NLog to .NET logging
public static class LoggingConfigurator
{
public static void ConfigureLogging(LoggingSettings loggingSettings)
{
if (NLog.LogManager.Configuration != null)
//used for loggers outside of ServiceControl (i.e. transports and core) to use the logger factory defined here
LogManager.UseFactory(new ExtensionsLoggerFactory(LoggerFactory.Create(configure => configure.BuildLogger(loggingSettings.LogLevel))));

if (!LoggerUtil.IsLoggingTo(Loggers.NLog) || NLog.LogManager.Configuration != null)
{
return;
}

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

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

NLog.LogManager.Configuration = nlogConfig;

LogManager.UseFactory(new ExtensionsLoggerFactory(LoggerFactory.Create(configure => configure.BuildLogger(loggingSettings.LogLevel))));

//using LogManager here rather than LoggerUtil.CreateStaticLogger since this is exclusive to NLog
var logger = LogManager.GetLogger("LoggingConfiguration");
var logEventInfo = new LogEventInfo { TimeStamp = DateTime.UtcNow };
var loggingTo = AppEnvironment.RunningInContainer ? "console" : fileTarget.FileName.Render(logEventInfo);
Expand Down
6 changes: 3 additions & 3 deletions src/ServiceControl.Infrastructure/LoggingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class LoggingSettings
{
public LoggingSettings(SettingsRootNamespace rootNamespace, LogLevel defaultLevel = LogLevel.Information, string logPath = null)
{
LogLevel = InitializeLogLevel(rootNamespace, defaultLevel);
LogPath = SettingsReader.Read(rootNamespace, logPathKey, Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));

var loggingProviders = (SettingsReader.Read<string>(rootNamespace, loggingProvidersKey) ?? "").Split(",");
var activeLoggers = Loggers.None;
if (loggingProviders.Contains("NLog"))
Expand All @@ -26,6 +23,9 @@ public LoggingSettings(SettingsRootNamespace rootNamespace, LogLevel defaultLeve
}
//this defaults to NLog because historically that was the default, and we don't want to break existing installs that don't have the config key to define loggingProviders
LoggerUtil.ActiveLoggers = activeLoggers == Loggers.None ? Loggers.NLog : activeLoggers;

LogLevel = InitializeLogLevel(rootNamespace, defaultLevel);
LogPath = SettingsReader.Read(rootNamespace, logPathKey, Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));
}

public LogLevel LogLevel { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
namespace ServiceControl
{
using NServiceBus.Logging;
using Microsoft.Extensions.Logging;

public class RavenDbLogLevelToLogsModeMapper
{
static readonly ILog Logger = LogManager.GetLogger(typeof(RavenDbLogLevelToLogsModeMapper));

public static string Map(string ravenDbLogLevel)
public static string Map(string ravenDbLogLevel, ILogger logger)
{
switch (ravenDbLogLevel.ToLower())
{
Expand All @@ -24,7 +22,7 @@ public static string Map(string ravenDbLogLevel)
case "operations":
return "Operations";
default:
Logger.WarnFormat("Unknown log level '{0}', mapped to 'Operations'", ravenDbLogLevel);
logger.LogWarning("Unknown log level '{RavenDbLogLevel}', mapped to 'Operations'", ravenDbLogLevel);
return "Operations";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NServiceBus;
using System;
using System.Threading;
using System.Threading.Tasks;
using Logging;
using Microsoft.Extensions.Logging;

/// <summary>
/// A circuit breaker that is armed on a failure and disarmed on success. After <see cref="timeToWaitBeforeTriggering"/> in the
Expand Down Expand Up @@ -44,13 +44,15 @@ public RepeatedFailuresOverTimeCircuitBreaker(
string name,
TimeSpan timeToWaitBeforeTriggering,
Action<Exception> triggerAction,
ILogger logger,
Action? armedAction = null,
Action? disarmedAction = null,
TimeSpan? timeToWaitWhenTriggered = default,
TimeSpan? timeToWaitWhenArmed = default)
{
this.name = name;
this.triggerAction = triggerAction;
this.logger = logger;
this.armedAction = armedAction ?? (static () => { });
this.disarmedAction = disarmedAction ?? (static () => { });
this.timeToWaitBeforeTriggering = timeToWaitBeforeTriggering;
Expand Down Expand Up @@ -82,14 +84,14 @@ public void Success()
circuitBreakerState = Disarmed;

_ = timer.Change(Timeout.Infinite, Timeout.Infinite);
Logger.InfoFormat("The circuit breaker for '{0}' is now disarmed.", name);
logger.LogInformation("The circuit breaker for '{BreakerName}' is now disarmed.", name);
try
{
disarmedAction();
}
catch (Exception ex)
{
Logger.Error($"The circuit breaker for '{name}' was unable to execute the disarm action.", ex);
logger.LogError(ex, "The circuit breaker for '{BreakerName}' was unable to execute the disarm action.", name);
throw;
}
}
Expand Down Expand Up @@ -130,23 +132,20 @@ public Task Failure(Exception exception, CancellationToken cancellationToken = d
}
catch (Exception ex)
{
Logger.Error($"The circuit breaker for '{name}' was unable to execute the arm action.", new AggregateException(ex, exception));
logger.LogError(new AggregateException(ex, exception), "The circuit breaker for '{BreakerName}' was unable to execute the arm action.", name);
throw;
}

_ = timer.Change(timeToWaitBeforeTriggering, NoPeriodicTriggering);
Logger.WarnFormat("The circuit breaker for '{0}' is now in the armed state due to '{1}' and might trigger in '{2}' when not disarmed.", name, exception, timeToWaitBeforeTriggering);
logger.LogWarning("The circuit breaker for '{BreakerName}' is now in the armed state due to '{BreakerCause}' and might trigger in '{BreakerTriggerTime}' when not disarmed.", name, exception, timeToWaitBeforeTriggering);
}

return Delay();

Task Delay()
{
var timeToWait = previousState == Triggered ? timeToWaitWhenTriggered : timeToWaitWhenArmed;
if (Logger.IsDebugEnabled)
{
Logger.DebugFormat("The circuit breaker for '{0}' is delaying the operation by '{1}'.", name, timeToWait);
}
logger.LogDebug("The circuit breaker for '{BreakerName}' is delaying the operation by '{BreakerTriggerTime}'.", name, timeToWait);
return Task.Delay(timeToWait, cancellationToken);
}
}
Expand All @@ -173,15 +172,15 @@ void CircuitBreakerTriggered(object? state)
}

circuitBreakerState = Triggered;
Logger.WarnFormat("The circuit breaker for '{0}' will now be triggered with exception '{1}'.", name, lastException);
logger.LogWarning("The circuit breaker for '{BreakerName}' will now be triggered with exception '{BreakerCause}'.", name, lastException);

try
{
triggerAction(lastException!);
}
catch (Exception ex)
{
Logger.Fatal($"The circuit breaker for '{name}' was unable to execute the trigger action.", new AggregateException(ex, lastException!));
logger.LogCritical(new AggregateException(ex, lastException!), "The circuit breaker for '{BreakerName}' was unable to execute the trigger action.", name);
}
}
}
Expand All @@ -204,5 +203,5 @@ void CircuitBreakerTriggered(object? state)
const int Triggered = 2;

static readonly TimeSpan NoPeriodicTriggering = TimeSpan.FromMilliseconds(-1);
static readonly ILog Logger = LogManager.GetLogger<RepeatedFailuresOverTimeCircuitBreaker>();
readonly ILogger logger;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Extensions.Logging;
using NUnit.Framework;

class TestContextAppender(string categoryName) : ILogger
class TestContextAppender(string categoryName, LogLevel level) : ILogger
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
Expand All @@ -13,7 +13,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
TestContext.Out.WriteLine($"{categoryName}: {formatter(state, exception)}");
}
}
public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Warning;
public bool IsEnabled(LogLevel logLevel) => logLevel >= level;

public IDisposable BeginScope<TState>(TState state) where TState : notnull => Disposable.Instance;

Expand Down
Loading