diff --git a/src/Serilog/Context/ImmutableStack.cs b/src/Serilog/Context/EnricherStack.cs similarity index 69% rename from src/Serilog/Context/ImmutableStack.cs rename to src/Serilog/Context/EnricherStack.cs index 429adf96e..12db2d356 100644 --- a/src/Serilog/Context/ImmutableStack.cs +++ b/src/Serilog/Context/EnricherStack.cs @@ -15,6 +15,7 @@ using System; using System.Collections; using System.Collections.Generic; +using Serilog.Core; // General-purpose type; not all features are used here. // ReSharper disable MemberCanBePrivate.Global @@ -22,16 +23,16 @@ namespace Serilog.Context { - class ImmutableStack : IEnumerable + class EnricherStack: IEnumerable { - readonly ImmutableStack _under; - readonly T _top; + readonly EnricherStack _under; + readonly ILogEventEnricher _top; - ImmutableStack() + EnricherStack() { } - ImmutableStack(ImmutableStack under, T top) + EnricherStack(EnricherStack under, ILogEventEnricher top) { _under = under ?? throw new ArgumentNullException(nameof(under)); Count = under.Count + 1; @@ -40,27 +41,27 @@ class ImmutableStack : IEnumerable public Enumerator GetEnumerator() => new(this); - IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this); + IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this); IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this); public int Count { get; } - public static ImmutableStack Empty { get; } = new(); + public static EnricherStack Empty { get; } = new(); public bool IsEmpty => _under == null; - public ImmutableStack Push(T t) => new(this, t); + public EnricherStack Push(ILogEventEnricher t) => new(this, t); - public T Top => _top; + public ILogEventEnricher Top => _top; - internal struct Enumerator : IEnumerator + internal struct Enumerator : IEnumerator { - readonly ImmutableStack _stack; - ImmutableStack _top; - T _current; + readonly EnricherStack _stack; + EnricherStack _top; + ILogEventEnricher _current; - public Enumerator(ImmutableStack stack) + public Enumerator(EnricherStack stack) { _stack = stack; _top = stack; @@ -82,7 +83,7 @@ public void Reset() _current = default; } - public T Current => _current; + public ILogEventEnricher Current => _current; object IEnumerator.Current => _current; diff --git a/src/Serilog/Context/LogContext.cs b/src/Serilog/Context/LogContext.cs index 8fab181a0..14370d657 100644 --- a/src/Serilog/Context/LogContext.cs +++ b/src/Serilog/Context/LogContext.cs @@ -52,12 +52,12 @@ namespace Serilog.Context public static class LogContext { #if ASYNCLOCAL - static readonly AsyncLocal> Data = new(); + static readonly AsyncLocal Data = new(); #elif REMOTING static readonly string DataSlotName = typeof(LogContext).FullName + "@" + Guid.NewGuid(); #else // DOTNET_51 [ThreadStatic] - static ImmutableStack Data; + static EnricherStack Data; #endif /// @@ -159,7 +159,7 @@ public static IDisposable Suspend() var stack = GetOrCreateEnricherStack(); var bookmark = new ContextStackBookmark(stack); - Enrichers = ImmutableStack.Empty; + Enrichers = EnricherStack.Empty; return bookmark; } @@ -169,18 +169,18 @@ public static IDisposable Suspend() /// public static void Reset() { - if (Enrichers != null && Enrichers != ImmutableStack.Empty) + if (Enrichers != null && Enrichers != EnricherStack.Empty) { - Enrichers = ImmutableStack.Empty; + Enrichers = EnricherStack.Empty; } } - static ImmutableStack GetOrCreateEnricherStack() + static EnricherStack GetOrCreateEnricherStack() { var enrichers = Enrichers; if (enrichers == null) { - enrichers = ImmutableStack.Empty; + enrichers = EnricherStack.Empty; Enrichers = enrichers; } return enrichers; @@ -189,7 +189,7 @@ static ImmutableStack GetOrCreateEnricherStack() internal static void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { var enrichers = Enrichers; - if (enrichers == null || enrichers == ImmutableStack.Empty) + if (enrichers == null || enrichers == EnricherStack.Empty) return; foreach (var enricher in enrichers) @@ -200,9 +200,9 @@ internal static void Enrich(LogEvent logEvent, ILogEventPropertyFactory property sealed class ContextStackBookmark : IDisposable { - readonly ImmutableStack _bookmark; + readonly EnricherStack _bookmark; - public ContextStackBookmark(ImmutableStack bookmark) + public ContextStackBookmark(EnricherStack bookmark) { _bookmark = bookmark; } @@ -215,7 +215,7 @@ public void Dispose() #if ASYNCLOCAL - static ImmutableStack Enrichers + static EnricherStack Enrichers { get => Data.Value; set => Data.Value = value; @@ -223,13 +223,13 @@ static ImmutableStack Enrichers #elif REMOTING - static ImmutableStack Enrichers + static EnricherStack Enrichers { get { var objectHandle = CallContext.LogicalGetData(DataSlotName) as ObjectHandle; - return objectHandle?.Unwrap() as ImmutableStack; + return objectHandle?.Unwrap() as EnricherStack; } set { @@ -269,7 +269,7 @@ public void Dispose() #else // DOTNET_51 - static ImmutableStack Enrichers + static EnricherStack Enrichers { get => Data; set => Data = value;