Ejemplo n.º 1
0
        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.NotNull(lastEvent);
                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());
            }
        }