|
4 | 4 | #if NET9_0_OR_GREATER
|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Concurrent;
|
7 |
| -using System.Collections.Generic; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
8 | 8 | using Microsoft.Extensions.Diagnostics;
|
9 | 9 | using Microsoft.Extensions.Logging;
|
10 |
| -using Microsoft.Extensions.Logging.Abstractions; |
11 | 10 | using Microsoft.Extensions.Options;
|
| 11 | +using Microsoft.Shared.Diagnostics; |
| 12 | +using static Microsoft.Extensions.Logging.ExtendedLogger; |
12 | 13 |
|
13 | 14 | namespace Microsoft.AspNetCore.Diagnostics.Logging;
|
14 | 15 |
|
15 | 16 | internal sealed class HttpRequestBuffer : ILoggingBuffer
|
16 | 17 | {
|
17 | 18 | private readonly IOptionsMonitor<HttpRequestBufferOptions> _options;
|
18 |
| - private readonly ConcurrentDictionary<IBufferedLogger, ConcurrentQueue<HttpRequestBufferedLogRecord>> _buffers; |
| 19 | + private readonly IOptionsMonitor<GlobalBufferOptions> _globalOptions; |
| 20 | + private readonly ConcurrentQueue<SerializedLogRecord> _buffer; |
19 | 21 | private readonly TimeProvider _timeProvider = TimeProvider.System;
|
| 22 | + private readonly IBufferSink _bufferSink; |
| 23 | + private readonly object _bufferCapacityLocker = new(); |
| 24 | + private DateTimeOffset _truncateAfter; |
20 | 25 | private DateTimeOffset _lastFlushTimestamp;
|
21 | 26 |
|
22 |
| - public HttpRequestBuffer(IOptionsMonitor<HttpRequestBufferOptions> options) |
| 27 | + public HttpRequestBuffer(IBufferSink bufferSink, |
| 28 | + IOptionsMonitor<HttpRequestBufferOptions> options, |
| 29 | + IOptionsMonitor<GlobalBufferOptions> globalOptions) |
23 | 30 | {
|
24 | 31 | _options = options;
|
25 |
| - _buffers = new ConcurrentDictionary<IBufferedLogger, ConcurrentQueue<HttpRequestBufferedLogRecord>>(); |
26 |
| - _lastFlushTimestamp = _timeProvider.GetUtcNow(); |
27 |
| - } |
| 32 | + _globalOptions = globalOptions; |
| 33 | + _bufferSink = bufferSink; |
| 34 | + _buffer = new ConcurrentQueue<SerializedLogRecord>(); |
28 | 35 |
|
29 |
| - internal HttpRequestBuffer(IOptionsMonitor<HttpRequestBufferOptions> options, TimeProvider timeProvider) |
30 |
| - : this(options) |
31 |
| - { |
32 |
| - _timeProvider = timeProvider; |
33 |
| - _lastFlushTimestamp = _timeProvider.GetUtcNow(); |
| 36 | + _truncateAfter = _timeProvider.GetUtcNow(); |
34 | 37 | }
|
35 | 38 |
|
36 |
| - public bool TryEnqueue( |
37 |
| - IBufferedLogger logger, |
| 39 | + [RequiresUnreferencedCode( |
| 40 | + "Calls Microsoft.Extensions.Logging.SerializedLogRecord.SerializedLogRecord(LogLevel, EventId, DateTimeOffset, IReadOnlyList<KeyValuePair<String, Object>>, Exception, String)")] |
| 41 | + public bool TryEnqueue<TState>( |
38 | 42 | LogLevel logLevel,
|
39 | 43 | string category,
|
40 | 44 | EventId eventId,
|
41 |
| - IReadOnlyList<KeyValuePair<string, object?>> joiner, |
| 45 | + TState attributes, |
42 | 46 | Exception? exception,
|
43 |
| - string formatter) |
| 47 | + Func<TState, Exception?, string> formatter) |
44 | 48 | {
|
45 | 49 | if (!IsEnabled(category, logLevel, eventId))
|
46 | 50 | {
|
47 | 51 | return false;
|
48 | 52 | }
|
49 | 53 |
|
50 |
| - var record = new HttpRequestBufferedLogRecord(logLevel, eventId, joiner, exception, formatter); |
51 |
| - var queue = _buffers.GetOrAdd(logger, _ => new ConcurrentQueue<HttpRequestBufferedLogRecord>()); |
52 |
| - |
53 |
| - // probably don't need to limit buffer capacity? |
54 |
| - // because buffer is disposed when the respective HttpContext is disposed |
55 |
| - // don't expect it to grow so much to cause a problem? |
56 |
| - if (queue.Count >= _options.CurrentValue.PerRequestCapacity) |
| 54 | + switch (attributes) |
57 | 55 | {
|
58 |
| - _ = queue.TryDequeue(out HttpRequestBufferedLogRecord? _); |
| 56 | + case ModernTagJoiner modernTagJoiner: |
| 57 | + _buffer.Enqueue(new SerializedLogRecord(logLevel, eventId, _timeProvider.GetUtcNow(), modernTagJoiner, exception, |
| 58 | + ((Func<ModernTagJoiner, Exception?, string>)(object)formatter)(modernTagJoiner, exception))); |
| 59 | + break; |
| 60 | + case LegacyTagJoiner legacyTagJoiner: |
| 61 | + _buffer.Enqueue(new SerializedLogRecord(logLevel, eventId, _timeProvider.GetUtcNow(), legacyTagJoiner, exception, |
| 62 | + ((Func<LegacyTagJoiner, Exception?, string>)(object)formatter)(legacyTagJoiner, exception))); |
| 63 | + break; |
| 64 | + default: |
| 65 | + Throw.ArgumentException(nameof(attributes), $"Unsupported type of the log attributes object detected: {typeof(TState)}"); |
| 66 | + break; |
59 | 67 | }
|
60 | 68 |
|
61 |
| - queue.Enqueue(record); |
| 69 | + var now = _timeProvider.GetUtcNow(); |
| 70 | + lock (_bufferCapacityLocker) |
| 71 | + { |
| 72 | + if (now >= _truncateAfter) |
| 73 | + { |
| 74 | + _truncateAfter = now.Add(_options.CurrentValue.PerRequestDuration); |
| 75 | + TruncateOverlimit(); |
| 76 | + } |
| 77 | + } |
62 | 78 |
|
63 | 79 | return true;
|
64 | 80 | }
|
65 | 81 |
|
| 82 | + [RequiresUnreferencedCode("Calls Microsoft.Extensions.Logging.BufferSink.LogRecords(IEnumerable<SerializedLogRecord>)")] |
66 | 83 | public void Flush()
|
67 | 84 | {
|
68 |
| - foreach (var (logger, queue) in _buffers) |
69 |
| - { |
70 |
| - var result = new List<BufferedLogRecord>(); |
71 |
| - while (!queue.IsEmpty) |
72 |
| - { |
73 |
| - if (queue.TryDequeue(out HttpRequestBufferedLogRecord? item)) |
74 |
| - { |
75 |
| - result.Add(item); |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - logger.LogRecords(result); |
80 |
| - } |
| 85 | + var result = _buffer.ToArray(); |
| 86 | + _buffer.Clear(); |
81 | 87 |
|
82 | 88 | _lastFlushTimestamp = _timeProvider.GetUtcNow();
|
| 89 | + |
| 90 | + _bufferSink.LogRecords(result); |
83 | 91 | }
|
84 | 92 |
|
85 | 93 | public bool IsEnabled(string category, LogLevel logLevel, EventId eventId)
|
86 | 94 | {
|
87 |
| - if (_timeProvider.GetUtcNow() < _lastFlushTimestamp + _options.CurrentValue.SuspendAfterFlushDuration) |
| 95 | + if (_timeProvider.GetUtcNow() < _lastFlushTimestamp + _globalOptions.CurrentValue.SuspendAfterFlushDuration) |
88 | 96 | {
|
89 | 97 | return false;
|
90 | 98 | }
|
91 | 99 |
|
92 |
| - LoggerFilterRuleSelector.Select<BufferFilterRule>(_options.CurrentValue.Rules, category, logLevel, eventId, out BufferFilterRule? rule); |
| 100 | + LoggerFilterRuleSelector.Select(_options.CurrentValue.Rules, category, logLevel, eventId, out BufferFilterRule? rule); |
93 | 101 |
|
94 | 102 | return rule is not null;
|
95 | 103 | }
|
| 104 | + |
| 105 | + public void TruncateOverlimit() |
| 106 | + { |
| 107 | + // Capacity is a soft limit, which might be exceeded, esp. in multi-threaded environments. |
| 108 | + while (_buffer.Count > _options.CurrentValue.PerRequestCapacity) |
| 109 | + { |
| 110 | + _ = _buffer.TryDequeue(out _); |
| 111 | + } |
| 112 | + } |
96 | 113 | }
|
97 | 114 | #endif
|
0 commit comments