forked from dotnet/dev-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestLogger.cs
More file actions
47 lines (37 loc) · 1.9 KB
/
Copy pathRequestLogger.cs
File metadata and controls
47 lines (37 loc) · 1.9 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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 DevProxy.Abstractions.Plugins;
using DevProxy.Abstractions.Proxy;
using DevProxy.Proxy;
using Microsoft.VisualStudio.Threading;
namespace DevProxy.Logging;
sealed class RequestLogger(IServiceProvider serviceProvider, IProxyState proxyState) : ILogger
{
private readonly IProxyState _proxyState = proxyState;
private readonly IServiceProvider _serviceProvider = serviceProvider;
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (state is RequestLog requestLog)
{
if (_proxyState.IsRecording)
{
_proxyState.RequestLogs.Enqueue(requestLog);
}
var requestLogArgs = new RequestLogArgs(requestLog);
using var joinableTaskContext = new JoinableTaskContext();
var joinableTaskFactory = new JoinableTaskFactory(joinableTaskContext);
// Lazily resolve plugins to avoid circular dependency
var plugins = _serviceProvider.GetRequiredService<IEnumerable<IPlugin>>();
foreach (var plugin in plugins.Where(p => p.Enabled))
{
// we don't have the app's cancellation token in the current
// implementation, but should it change in the future,
// we won't have to break the interface
joinableTaskFactory.Run(async () => await plugin.AfterRequestLogAsync(requestLogArgs, CancellationToken.None));
}
}
}
}