| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Xunit.Abstractions; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Testing.Shared; |
| | | 5 | | |
| | 0 | 6 | | public class XunitLogger(ITestOutputHelper testOutputHelper, string categoryName) : ILogger |
| | | 7 | | { |
| | 0 | 8 | | public IDisposable BeginScope<TState>(TState state) where TState : notnull => NoopDisposable.Instance; |
| | | 9 | | |
| | | 10 | | public bool IsEnabled(LogLevel logLevel) |
| | 0 | 11 | | => true; |
| | | 12 | | |
| | | 13 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Excepti |
| | | 14 | | { |
| | | 15 | | try |
| | | 16 | | { |
| | 0 | 17 | | testOutputHelper.WriteLine($"{categoryName} [{eventId}] {formatter(state, exception)}"); |
| | | 18 | | |
| | 0 | 19 | | if (exception != null) |
| | 0 | 20 | | testOutputHelper.WriteLine(exception.ToString()); |
| | 0 | 21 | | } |
| | 0 | 22 | | catch (InvalidOperationException) |
| | | 23 | | { |
| | | 24 | | // Suppress "no currently active test" exceptions that can occur when background tasks |
| | | 25 | | // (like timers) try to log after tests have completed |
| | 0 | 26 | | } |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | private class NoopDisposable : IDisposable |
| | | 30 | | { |
| | 0 | 31 | | public static readonly NoopDisposable Instance = new(); |
| | | 32 | | |
| | | 33 | | public void Dispose() |
| | | 34 | | { |
| | 0 | 35 | | } |
| | | 36 | | } |
| | | 37 | | } |