forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLogger.cs
More file actions
29 lines (20 loc) · 1.14 KB
/
TestLogger.cs
File metadata and controls
29 lines (20 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
namespace Yarp.ReverseProxy.Common;
public sealed class TestLogger(ILogger xunitLogger, string categoryName) : ILogger
{
public record LogEntry(string CategoryName, LogLevel LogLevel, EventId EventId, string Message, Exception Exception);
private static readonly AsyncLocal<List<LogEntry>> _logsAsyncLocal = new();
public static List<LogEntry> Collect() => _logsAsyncLocal.Value ??= [];
public IDisposable BeginScope<TState>(TState state) where TState : notnull => xunitLogger.BeginScope(state);
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
_logsAsyncLocal.Value?.Add(new LogEntry(categoryName, logLevel, eventId, formatter(state, exception), exception));
xunitLogger.Log(logLevel, eventId, state, exception, formatter);
}
}