Add optional logging scope support#86
Open
Arithmomaniac wants to merge 2 commits into
Open
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f918226d-1eb0-4d36-9d9a-397347a05d4a
Avoids the StringBuilder-to-ValueStringBuilder copy and its netstandard2.0 fallback. The list holds one entry per active scope, so it needs no capacity trimming. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f918226d-1eb0-4d36-9d9a-397347a05d4a
Contributor
|
@Arithmomaniac looks good! Do I understand correctly that the new |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds opt-in logging scope support as requested in #77. Setting
IncludeScopesadds active scopes as a tab-separated field before the message, formatted in ConsoleLogger-style outer-to-inner order:=> Outer => Inner. Scopes are disabled by default, and entries without active scopes retain the existing output. Scope context is also included on exception-only entries.The main technical constraint is that
IExternalScopeProviderexposes scopes only through its callback-basedForEachScopeAPI, while the existing low-allocationValueStringBuilderis aref structand cannot be captured or passed as callback state. Scopes are therefore collected into a reusable list and then appended directly to the final buffer, which also keeps the exact output length known up front so the buffer is still rented once. The list holds one entry per active scope, so its capacity stays as small as the scope nesting. It is checked out while in use so logging from a scope'sToString()cannot corrupt another entry being formatted on the same thread.FileLoggerProviderimplementsISupportExternalScope, allowingLoggerFactoryto supply the shared provider that tracksBeginScope()state across nesting, disposal, and asynchronous flow.FileLogger.BeginScope()delegates to it for direct logger usage. CustomFormatLogEntrydelegates continue receiving the unchangedLogMessageand do not include scopes automatically.Documentation and tests cover configuration, scope ordering and lifetime, exceptions, custom formatting, long and null scopes, and logging during scope formatting. The library, tests, and examples build successfully, and all 47 tests pass on
net481,net8.0, andnet10.0.Fixes #77