Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename ImmutableStack<ILogEventEnricher> to EnricherStack #1728

Merged
merged 1 commit into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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