// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Concurrent; namespace Microsoft.Extensions.Logging.Testing { public class TestSink : ITestSink { private ConcurrentQueue _scopes; private ConcurrentQueue _writes; public TestSink( Func writeEnabled = null, Func beginEnabled = null) { WriteEnabled = writeEnabled; BeginEnabled = beginEnabled; _scopes = new ConcurrentQueue(); _writes = new ConcurrentQueue(); } public Func WriteEnabled { get; set; } public Func BeginEnabled { get; set; } public IProducerConsumerCollection Scopes { get => _scopes; set => _scopes = new ConcurrentQueue(value); } public IProducerConsumerCollection Writes { get => _writes; set => _writes = new ConcurrentQueue(value); } public event Action MessageLogged; public event Action ScopeStarted; public void Write(WriteContext context) { if (WriteEnabled == null || WriteEnabled(context)) { _writes.Enqueue(context); } MessageLogged?.Invoke(context); } public void Begin(BeginScopeContext context) { if (BeginEnabled == null || BeginEnabled(context)) { _scopes.Enqueue(context); } ScopeStarted?.Invoke(context); } public static bool EnableWithTypeName(WriteContext context) { return context.LoggerName.Equals(typeof(T).FullName); } public static bool EnableWithTypeName(BeginScopeContext context) { return context.LoggerName.Equals(typeof(T).FullName); } } }