Skip to content

Commit

Permalink
rename ImmutableStack<ILogEventEnricher> to EnricherStack (#1728)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Aug 23, 2022
1 parent 635667b commit fb964f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Expand Up @@ -15,23 +15,24 @@
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
// ReSharper disable MemberCanBeProtected.Global

namespace Serilog.Context
{
class ImmutableStack<T> : IEnumerable<T>
class EnricherStack: IEnumerable<ILogEventEnricher>
{
readonly ImmutableStack<T> _under;
readonly T _top;
readonly EnricherStack _under;
readonly ILogEventEnricher _top;

ImmutableStack()
EnricherStack()
{
}

ImmutableStack(ImmutableStack<T> under, T top)
EnricherStack(EnricherStack under, ILogEventEnricher top)
{
_under = under ?? throw new ArgumentNullException(nameof(under));
Count = under.Count + 1;
Expand All @@ -40,27 +41,27 @@ class ImmutableStack<T> : IEnumerable<T>

public Enumerator GetEnumerator() => new(this);

IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
IEnumerator<ILogEventEnricher> IEnumerable<ILogEventEnricher>.GetEnumerator() => new Enumerator(this);

IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);

public int Count { get; }

public static ImmutableStack<T> Empty { get; } = new();
public static EnricherStack Empty { get; } = new();

public bool IsEmpty => _under == null;

public ImmutableStack<T> 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<T>
internal struct Enumerator : IEnumerator<ILogEventEnricher>
{
readonly ImmutableStack<T> _stack;
ImmutableStack<T> _top;
T _current;
readonly EnricherStack _stack;
EnricherStack _top;
ILogEventEnricher _current;

public Enumerator(ImmutableStack<T> stack)
public Enumerator(EnricherStack stack)
{
_stack = stack;
_top = stack;
Expand All @@ -82,7 +83,7 @@ public void Reset()
_current = default;
}

public T Current => _current;
public ILogEventEnricher Current => _current;

object IEnumerator.Current => _current;

Expand Down
28 changes: 14 additions & 14 deletions src/Serilog/Context/LogContext.cs
Expand Up @@ -52,12 +52,12 @@ namespace Serilog.Context
public static class LogContext
{
#if ASYNCLOCAL
static readonly AsyncLocal<ImmutableStack<ILogEventEnricher>> Data = new();
static readonly AsyncLocal<EnricherStack> Data = new();
#elif REMOTING
static readonly string DataSlotName = typeof(LogContext).FullName + "@" + Guid.NewGuid();
#else // DOTNET_51
[ThreadStatic]
static ImmutableStack<ILogEventEnricher> Data;
static EnricherStack Data;
#endif

/// <summary>
Expand Down Expand Up @@ -159,7 +159,7 @@ public static IDisposable Suspend()
var stack = GetOrCreateEnricherStack();
var bookmark = new ContextStackBookmark(stack);

Enrichers = ImmutableStack<ILogEventEnricher>.Empty;
Enrichers = EnricherStack.Empty;

return bookmark;
}
Expand All @@ -169,18 +169,18 @@ public static IDisposable Suspend()
/// </summary>
public static void Reset()
{
if (Enrichers != null && Enrichers != ImmutableStack<ILogEventEnricher>.Empty)
if (Enrichers != null && Enrichers != EnricherStack.Empty)
{
Enrichers = ImmutableStack<ILogEventEnricher>.Empty;
Enrichers = EnricherStack.Empty;
}
}

static ImmutableStack<ILogEventEnricher> GetOrCreateEnricherStack()
static EnricherStack GetOrCreateEnricherStack()
{
var enrichers = Enrichers;
if (enrichers == null)
{
enrichers = ImmutableStack<ILogEventEnricher>.Empty;
enrichers = EnricherStack.Empty;
Enrichers = enrichers;
}
return enrichers;
Expand All @@ -189,7 +189,7 @@ static ImmutableStack<ILogEventEnricher> GetOrCreateEnricherStack()
internal static void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var enrichers = Enrichers;
if (enrichers == null || enrichers == ImmutableStack<ILogEventEnricher>.Empty)
if (enrichers == null || enrichers == EnricherStack.Empty)
return;

foreach (var enricher in enrichers)
Expand All @@ -200,9 +200,9 @@ internal static void Enrich(LogEvent logEvent, ILogEventPropertyFactory property

sealed class ContextStackBookmark : IDisposable
{
readonly ImmutableStack<ILogEventEnricher> _bookmark;
readonly EnricherStack _bookmark;

public ContextStackBookmark(ImmutableStack<ILogEventEnricher> bookmark)
public ContextStackBookmark(EnricherStack bookmark)
{
_bookmark = bookmark;
}
Expand All @@ -215,21 +215,21 @@ public void Dispose()

#if ASYNCLOCAL

static ImmutableStack<ILogEventEnricher> Enrichers
static EnricherStack Enrichers
{
get => Data.Value;
set => Data.Value = value;
}

#elif REMOTING

static ImmutableStack<ILogEventEnricher> Enrichers
static EnricherStack Enrichers
{
get
{
var objectHandle = CallContext.LogicalGetData(DataSlotName) as ObjectHandle;

return objectHandle?.Unwrap() as ImmutableStack<ILogEventEnricher>;
return objectHandle?.Unwrap() as EnricherStack;
}
set
{
Expand Down Expand Up @@ -269,7 +269,7 @@ public void Dispose()

#else // DOTNET_51

static ImmutableStack<ILogEventEnricher> Enrichers
static EnricherStack Enrichers
{
get => Data;
set => Data = value;
Expand Down

0 comments on commit fb964f8

Please sign in to comment.