Skip to content

Commit

Permalink
Use AsyncLocal to find the correct ModuleLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst committed Jul 27, 2024
1 parent 186719a commit faa637d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
- Show exception (if one was thrown) within the GitHub markdown summary

# Breaking
- GitVersion.yml has been updated to v6. If using the `GitVersionInformation` model, some properties may have been added/removed/changed.
- Use `AsyncLocal` to find the correct `IModuleLogger` when using the `IModuleLoggerProvider` helper.
7 changes: 5 additions & 2 deletions src/ModularPipelines/Engine/ModuleExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ModularPipelines.Attributes;
using ModularPipelines.Exceptions;
using ModularPipelines.Extensions;
using ModularPipelines.Logging;
using ModularPipelines.Models;
using ModularPipelines.Modules;
using ModularPipelines.Options;
Expand Down Expand Up @@ -150,7 +151,7 @@ private Task<ModuleBase> StartModule(ModuleBase module)
{
lock (_moduleDictionaryLock)
{
return _moduleExecutionTasks.GetOrAdd(module, async @base =>
return _moduleExecutionTasks.GetOrAdd(module, @base => Task.Run(async () =>
{
_logger.LogDebug("Starting Module {Module}", module.GetType().Name);

Expand All @@ -163,6 +164,8 @@ private Task<ModuleBase> StartModule(ModuleBase module)

try
{
ModuleLogger.Values.Value = module.Context.Logger;

await _pipelineSetupExecutor.OnBeforeModuleStartAsync(module);

await module.StartInternal();
Expand All @@ -180,7 +183,7 @@ private Task<ModuleBase> StartModule(ModuleBase module)
await _moduleDisposer.DisposeAsync(module);
}
}
});
}));
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/ModularPipelines/Logging/ModuleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ namespace ModularPipelines.Logging;

internal abstract class ModuleLogger : IModuleLogger
{
internal static readonly AsyncLocal<IModuleLogger> Values = new();

protected static readonly object DisposeLock = new();
protected static readonly object LogLock = new();
protected Exception? _exception;

internal DateTime LastLogWritten { get; set; } = DateTime.MinValue;

public abstract void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter);
Expand All @@ -34,7 +36,6 @@ internal class ModuleLogger<T> : ModuleLogger, IModuleLogger, ILogger<T>
{
private readonly ILogger<T> _defaultLogger;
private readonly ISecretObfuscator _secretObfuscator;
private readonly ISecretProvider _secretProvider;
private readonly IConsoleWriter _consoleWriter;
private readonly ISmartCollapsableLoggingStringBlockProvider _collapsableLoggingStringBlockProvider;

Expand All @@ -46,13 +47,11 @@ internal class ModuleLogger<T> : ModuleLogger, IModuleLogger, ILogger<T>
public ModuleLogger(ILogger<T> defaultLogger,
IModuleLoggerContainer moduleLoggerContainer,
ISecretObfuscator secretObfuscator,
ISecretProvider secretProvider,
IConsoleWriter consoleWriter,
ISmartCollapsableLoggingStringBlockProvider collapsableLoggingStringBlockProvider)
{
_defaultLogger = defaultLogger;
_secretObfuscator = secretObfuscator;
_secretProvider = secretProvider;
_consoleWriter = consoleWriter;
_collapsableLoggingStringBlockProvider = collapsableLoggingStringBlockProvider;
moduleLoggerContainer.AddLogger(this);
Expand Down
5 changes: 5 additions & 0 deletions src/ModularPipelines/Logging/ModuleLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public IModuleLogger GetLogger()
{
return _moduleLogger;
}

if (ModuleLogger.Values.Value != null)
{
return _moduleLogger = ModuleLogger.Values.Value;
}

var stackFrames = new StackTrace().GetFrames().ToList();

Expand Down

0 comments on commit faa637d

Please sign in to comment.