X Tutup
The Wayback Machine - https://web.archive.org/web/20201124034609/https://github.com/NLog/NLog/pull/4124
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScopeContext to replace MDLC and NDLC #4124

Open
wants to merge 3 commits into
base: dev
from

Conversation

@snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Sep 30, 2020

Resolves #4056

ScopeContext has some small behavior adjustments:

  • Dictionary is case-insensitive
  • Dictionary is stacked, so it can restore old values
    • Special legacy-mode activates for MappedDiagnosticsLogicalContext.Set, but can still restore
    • Special legacy-mode activates for NestedDiagnosticsLogicalContext.Pop, but can still restore
  • ScopeContext-Timing only works on NetStandard / Net46 (Because legacy CallContext do not like unknown NLog types)
    • Added unit-test NdlcGetAllMessages that explodes when putting non-Microsoft objects into the CallContext. If adding this unit-test to master-branch then it will fail because NLog own objects are not welcome in the CallContext.

ScopeContext has better performance since it performs lazy allocation of Dictionary (To ensure unique properties) until it actually has to write the log-message. And NLog PR now only uses a single AsyncLocal-variable to capture the BeginScope-state (Instead of one for NDLC and one for MDLC).

Because MDLC (and NDLC) are well-known constructs in the NLog-universe, then I prefer that ${mdlc} and ${ndlc} continue to work when people using NLog.Extension.Logging upgrade to lastest NLog 5.0 (Along with IncludeMdlc on JsonLayout), Then over time people can be convinced to move to the new ${scopecontext} (New PR).

Very happy that all the existing unit-test for MDLC + NDLC are working without any modifications. ScopeContext is also able to handle the more exotic methods of adding / removing properties within an active scope.

@snakefoot snakefoot added this to the 5.0 (new) milestone Sep 30, 2020
@snakefoot snakefoot force-pushed the snakefoot:ThreadExecutionContext branch from 3d8ebd5 to e3d7a3b Sep 30, 2020
@snakefoot snakefoot added the feature label Sep 30, 2020
@snakefoot snakefoot force-pushed the snakefoot:ThreadExecutionContext branch 5 times, most recently from f163008 to d3d5d31 Sep 30, 2020
@snakefoot snakefoot force-pushed the snakefoot:ThreadExecutionContext branch from d3d5d31 to 48492d8 Oct 2, 2020
@sonarcloud
Copy link

@sonarcloud sonarcloud bot commented Oct 2, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 5 Code Smells

82.1% 82.1% Coverage
0.0% 0.0% Duplication

@snakefoot
Copy link
Contributor Author

@snakefoot snakefoot commented Oct 3, 2020

After looking at NLog/NLog.Extensions.Logging#445 then I think further building blocks can be made:

  • Add the missing ScopeProperties-LayoutRenderer (MLDC) and ScopeOperations-LayoutRenderer (NDLC).
  • Extend Logger-object with methods PushScopeProperty and PushScopeOperation
  • Add DefaultScopeProperties-dictionary to LoggingConfiguration (ThreadSafeDictionary<string, Layout>).
    • Fix locations that support IncludeScopeProperties to retrieve the ScopeProperties from the LogFactory.
  • Extend LoggingConfigurationParser to handle default-scope-properties. Maybe in a way so it avoids all the pitfalls discovered with NLog-config-variables together with autoreload.

But all that can come with new PullRequests.

@304NotModified 304NotModified force-pushed the NLog:dev branch from 28ce43f to f1656a4 Oct 3, 2020
///
/// .NetCore (and .Net46) uses AsyncLocal for handling the thread execution context. Older .NetFramework uses System.Runtime.Remoting.CallContext
/// </remarks>
public static class ScopeContext

This comment has been minimized.

@304NotModified

304NotModified Oct 6, 2020
Member

I think it's wise to move to a non-static class with an interface and a Default property. We need that for unit testing/mocking/DI In the future I also like to add GlobalContext (the same, non static and interfaces)

This comment has been minimized.

@snakefoot

snakefoot Oct 6, 2020
Author Contributor

Both CallContext and AsyncLocal are static-variables, and will not disappear by living in a singleton. I was planning to let the Logger-object include the methods PushScopeProperty and PushScopeOperation, and do the mocking with Logger-object (Will also allows preconfigured-scope-properties using Layout-logic).

/// Creates logical operation scope that includes provided properties and stack-value
/// </summary>
/// <param name="operationState">Value to added to the scope operation stack</param>
/// <param name="properties">Properties being added to the scope dictionray</param>

This comment has been minimized.

@304NotModified

304NotModified Oct 6, 2020
Member

dictionray ;)

This comment has been minimized.

@snakefoot

snakefoot Oct 6, 2020
Author Contributor

Thanks. Fixed spelling. But Azure-build is now in cancelled state. Can you give it a kick? (Instead of me force-pushing)

@304NotModified
Copy link
Member

@304NotModified 304NotModified commented Oct 6, 2020

Maybe I missing something, but what's the difference between property and operationProperty?

@snakefoot
Copy link
Contributor Author

@snakefoot snakefoot commented Oct 6, 2020

Maybe I missing something, but what's the difference between property and operationProperty?

ScopeProperty (MDLC) is name + value dictionary. Ex. "RequestId" = Guid.NewGuid().

ScopeOperationState (NDLC) is operation-description as stack. Ex.

  • "Processing order request"
    • "Saving order to database"
… and PushOperationProperties
@snakefoot
Copy link
Contributor Author

@snakefoot snakefoot commented Nov 19, 2020

@304NotModified Anything one can do to get the ball rolling on this one. Would like to continue on with the todo-list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.
X Tutup