Skip to content

Commit

Permalink
Merge pull request serilog#1007 from mariozski/dev
Browse files Browse the repository at this point in the history
Implementation of serilog#1006
  • Loading branch information
nblumhardt authored Jul 28, 2017
2 parents f989fd6 + 118f869 commit 67ff309
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Serilog/Context/LogContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2015 Serilog Contributors
// Copyright 2013-2015 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -150,6 +150,32 @@ public static ILogEventEnricher Clone()
return new SafeAggregateEnricher(stack);
}

/// <summary>
/// Remove all enrichers from the <see cref="LogContext"/>, returning an <see cref="IDisposable"/>
/// that must later be used to restore enrichers that were on the stack before <see cref="Suspend"/> was called.
/// </summary>
/// <returns>A token that must be disposed, in order, to restore properties back to the stack.</returns>
public static IDisposable Suspend()
{
var stack = GetOrCreateEnricherStack();
var bookmark = new ContextStackBookmark(stack);

Enrichers = ImmutableStack<ILogEventEnricher>.Empty;

return bookmark;
}

/// <summary>
/// Remove all enrichers from <see cref="LogContext"/> for the current async scope.
/// </summary>
public static void Reset()
{
if (Enrichers != null && Enrichers != ImmutableStack<ILogEventEnricher>.Empty)
{
Enrichers = ImmutableStack<ILogEventEnricher>.Empty;
}
}

static ImmutableStack<ILogEventEnricher> GetOrCreateEnricherStack()
{
var enrichers = Enrichers;
Expand Down Expand Up @@ -250,4 +276,4 @@ static ImmutableStack<ILogEventEnricher> Enrichers
}
#endif
}
}
}
56 changes: 56 additions & 0 deletions test/Serilog.Tests/Context/LogContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,62 @@ public async Task ContextPropertiesCrossAsyncCalls()
}
}

[Fact]
public async Task ContextEnrichersInAsyncScopeCanBeCleared()
{
LogEvent lastEvent = null;

var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Sink(new DelegatingSink(e => lastEvent = e))
.CreateLogger();

using (LogContext.Push(new PropertyEnricher("A", 1)))
{
await Task.Run(() =>
{
LogContext.Reset();
log.Write(Some.InformationEvent());
});

Assert.Empty(lastEvent.Properties);

// Reset should only work for current async scope, outside of it previous Context
// instance should be available again.
log.Write(Some.InformationEvent());
Assert.Equal(1, lastEvent.Properties["A"].LiteralValue());
}
}

[Fact]
public async Task ContextEnrichersCanBeTemporarilyCleared()
{
LogEvent lastEvent = null;

var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Sink(new DelegatingSink(e => lastEvent = e))
.CreateLogger();

using (LogContext.Push(new PropertyEnricher("A", 1)))
{
using (LogContext.Suspend())
{
await Task.Run(() =>
{
log.Write(Some.InformationEvent());
});

Assert.Empty(lastEvent.Properties);
}

// Suspend should only work for scope of using. After calling Dispose all enrichers
// should be restored.
log.Write(Some.InformationEvent());
Assert.Equal(1, lastEvent.Properties["A"].LiteralValue());
}
}

#if APPDOMAIN
// Must not actually try to pass context across domains,
// since user property types may not be serializable.
Expand Down

0 comments on commit 67ff309

Please sign in to comment.