-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
988c709
commit 9d61d12
Showing
36 changed files
with
1,080 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...braries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
#if NET9_0_OR_GREATER | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Diagnostics.Logging; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Diagnostics.Buffering; | ||
internal sealed class HttpRequestBufferManager : IHttpRequestBufferManager | ||
{ | ||
private readonly GlobalBufferManager _globalBufferManager; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IOptionsMonitor<HttpRequestBufferOptions> _requestOptions; | ||
|
||
public HttpRequestBufferManager( | ||
GlobalBufferManager globalBufferManager, | ||
IHttpContextAccessor httpContextAccessor, | ||
IOptionsMonitor<HttpRequestBufferOptions> requestOptions) | ||
{ | ||
_globalBufferManager = globalBufferManager; | ||
_httpContextAccessor = httpContextAccessor; | ||
_requestOptions = requestOptions; | ||
} | ||
|
||
public ILoggingBuffer CreateBuffer(IBufferSink bufferSink, string category) | ||
{ | ||
var httpContext = _httpContextAccessor.HttpContext; | ||
if (httpContext is null) | ||
{ | ||
return _globalBufferManager.CreateBuffer(bufferSink, category); | ||
} | ||
|
||
if (!httpContext.Items.TryGetValue(category, out var buffer)) | ||
{ | ||
var httpRequestBuffer = new HttpRequestBuffer(bufferSink, _requestOptions); | ||
httpContext.Items[category] = httpRequestBuffer; | ||
return httpRequestBuffer; | ||
} | ||
|
||
if (buffer is not ILoggingBuffer loggingBuffer) | ||
{ | ||
throw new InvalidOperationException($"Unable to parse value of {buffer} of the {category}"); | ||
} | ||
|
||
return loggingBuffer; | ||
} | ||
|
||
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Logging.ILoggingBuffer.Flush()")] | ||
public void Flush() => _globalBufferManager.Flush(); | ||
|
||
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Logging.ILoggingBuffer.Flush()")] | ||
public void FlushCurrentRequestLogs() | ||
{ | ||
if (_httpContextAccessor.HttpContext is not null) | ||
{ | ||
foreach (var kvp in _httpContextAccessor.HttpContext!.Items) | ||
{ | ||
if (kvp.Value is ILoggingBuffer buffer) | ||
{ | ||
buffer.Flush(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Logging.ILoggingBuffer.TryEnqueue<TState>(LogLevel, String, EventId, TState, Exception, Func<TState, Exception, String>)")] | ||
public bool TryEnqueue<TState>( | ||
IBufferSink bufferSink, | ||
LogLevel logLevel, | ||
string category, | ||
EventId eventId, | ||
TState attributes, | ||
Exception? exception, | ||
Func<TState, Exception?, string> formatter) | ||
{ | ||
var buffer = CreateBuffer(bufferSink, category); | ||
return buffer.TryEnqueue(logLevel, category, eventId, attributes, exception, formatter); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
...raries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferProvider.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.