|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using Microsoft.Extensions.Logging.Abstractions; |
| 5 | +using Xunit.Abstractions; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.SpaServices.Extensions.Tests |
| 8 | +{ |
| 9 | + public class ListLoggerFactory : ILoggerFactory |
| 10 | + { |
| 11 | + private readonly Func<string, bool> _shouldLogCategory; |
| 12 | + private bool _disposed; |
| 13 | + |
| 14 | + public ListLoggerFactory() |
| 15 | + : this(_ => true) |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + public ListLoggerFactory(Func<string, bool> shouldLogCategory) |
| 20 | + { |
| 21 | + _shouldLogCategory = shouldLogCategory; |
| 22 | + Logger = new ListLogger(); |
| 23 | + } |
| 24 | + |
| 25 | + public List<(LogLevel Level, EventId Id, string Message, object State, Exception Exception)> Log => Logger.LoggedEvents; |
| 26 | + protected ListLogger Logger { get; set; } |
| 27 | + |
| 28 | + public virtual void Clear() => Logger.Clear(); |
| 29 | + |
| 30 | + public void SetTestOutputHelper(ITestOutputHelper testOutputHelper) |
| 31 | + { |
| 32 | + Logger.TestOutputHelper = testOutputHelper; |
| 33 | + } |
| 34 | + |
| 35 | + public virtual ILogger CreateLogger(string name) |
| 36 | + { |
| 37 | + CheckDisposed(); |
| 38 | + |
| 39 | + return !_shouldLogCategory(name) |
| 40 | + ? (ILogger)NullLogger.Instance |
| 41 | + : Logger; |
| 42 | + } |
| 43 | + |
| 44 | + private void CheckDisposed() |
| 45 | + { |
| 46 | + if (_disposed) |
| 47 | + { |
| 48 | + throw new ObjectDisposedException(nameof(ListLoggerFactory)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public void AddProvider(ILoggerProvider provider) |
| 53 | + { |
| 54 | + CheckDisposed(); |
| 55 | + } |
| 56 | + |
| 57 | + public void Dispose() |
| 58 | + { |
| 59 | + _disposed = true; |
| 60 | + } |
| 61 | + |
| 62 | + protected class ListLogger : ILogger |
| 63 | + { |
| 64 | + private readonly object _sync = new object(); |
| 65 | + |
| 66 | + public ITestOutputHelper TestOutputHelper { get; set; } |
| 67 | + |
| 68 | + public List<(LogLevel, EventId, string, object, Exception)> LoggedEvents { get; } |
| 69 | + = new List<(LogLevel, EventId, string, object, Exception)>(); |
| 70 | + |
| 71 | + public void Clear() |
| 72 | + { |
| 73 | + lock (_sync) // Guard against tests with explicit concurrency |
| 74 | + { |
| 75 | + LoggedEvents.Clear(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void Log<TState>( |
| 80 | + LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
| 81 | + { |
| 82 | + lock (_sync) // Guard against tests with explicit concurrency |
| 83 | + { |
| 84 | + var message = formatter(state, exception)?.Trim(); |
| 85 | + if (message != null) |
| 86 | + { |
| 87 | + TestOutputHelper?.WriteLine(message + Environment.NewLine); |
| 88 | + } |
| 89 | + |
| 90 | + LoggedEvents.Add((logLevel, eventId, message, state, exception)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public bool IsEnabled(LogLevel logLevel) => true; |
| 95 | + |
| 96 | + public IDisposable BeginScope(object state) => null; |
| 97 | + |
| 98 | + public IDisposable BeginScope<TState>(TState state) => null; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments