| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Concurrent; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace Helpers |
| | | 10 | | { |
| | | 11 | | public class ExceptionCapturingLogger : ILogger |
| | | 12 | | { |
| | | 13 | | private readonly ILogger _wrappedLogger; |
| | 2241 | 14 | | private readonly ConcurrentBag<Exception> _exceptionsLogged = new(); |
| | 2241 | 15 | | public ExceptionCapturingLogger(ILogger wrappedLogger) |
| | | 16 | | { |
| | 2241 | 17 | | _wrappedLogger = wrappedLogger; |
| | 2241 | 18 | | } |
| | | 19 | | |
| | | 20 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exce |
| | | 21 | | { |
| | 79 | 22 | | _wrappedLogger?.Log(logLevel, eventId, state, exception, formatter); |
| | 79 | 23 | | if (exception != null) |
| | | 24 | | { |
| | 2 | 25 | | _exceptionsLogged.Add(exception); |
| | | 26 | | } |
| | 79 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public bool IsEnabled(LogLevel logLevel) => _wrappedLogger?.IsEnabled(logLevel) ?? (int)logLevel>= (int)LogLevel |
| | | 30 | | |
| | 79 | 31 | | public IDisposable BeginScope<TState>(TState state) => NoopDisposable.Instance; |
| | 29 | 32 | | public IEnumerable<Exception> ExceptionsLogged => _exceptionsLogged; |
| | | 33 | | private class NoopDisposable : IDisposable |
| | | 34 | | { |
| | 1 | 35 | | public static NoopDisposable Instance = new NoopDisposable(); |
| | | 36 | | public void Dispose() |
| | 79 | 37 | | { } |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | } |