You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to 'set' or otherwise 'manage' a logger scope so that it is automagically propagated to all sub-components that resolve their own discrete ILogger implementations.
i.e.
Component A (i.e. a top-level "job")
has it's own ILogger implementation
has a dictionary of values to set for the logger scope. Would be using BeginScope(scopeItemsDictionary)
i.e. JobName, JobId, JobQueue, etc.
was injected with 1 or more sub-components as dependencies
i.e. "SomeJobDependencyComponent"
Component B (i.e. some logic that's used by the job)
has it's own ILogger implementation.
may or may not want to further add to the logger scope/items for log messages emit from itself
may or may not have further children sub-components/dependencies where the same logger scope propagation would be desired.
When I set the logger scope in component A, I'd want that same scope to be applied to component B's logger/logging and to apply to any further children components.
Managing the scope at the time of resolving the logger for constructor injection wouldn't work as the scope would not have been applied by the parent (component A) at that time.
So the 'resolving' of the actual scope/items to be applied for a given logger call (i.e. LogInformation) would need to be evaluated at the time of the logging, not when the logger instance was resolved/instantiated/injected to the components.
What I've explored so far:
I've investigated what native facilities exist but I'm not finding anything that would yield this type of behavior. I did find something close it seemed via the IExternalScopeProvider and that was not resolvable so I tried LoggerExternalScopeProvider and that too wouldn't resolve. I was using a simple services.AddLogging(..) call and expected (hoped) to be able to access those somehow and use the Push(..) method to add my items.
This seemed like it should have been fairly close to what I needed since it was using AsyncLocal to store the current state and then a push/pop (via dispose) approach but given that I couldn't resolve those types and the resolved ILoggerFactory doesn't allow me to access it either - I found myself blocked in pursuing those avenues.
We do use Serilog to back our logging and I believe something does exist with it that would give me this type of behavior but I don't want to couple to Serilog directly, I want to stick with the native MS logging types and extension methods, etc.
Has anyone else implemented this and if so, how and what did you leverage? I'm evaluating building our own but I'd much rather leverage something 'official' than homebrew our own.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I want to 'set' or otherwise 'manage' a logger scope so that it is automagically propagated to all sub-components that resolve their own discrete ILogger implementations.
i.e.
Component A (i.e. a top-level "job")
Component B (i.e. some logic that's used by the job)
When I set the logger scope in component A, I'd want that same scope to be applied to component B's logger/logging and to apply to any further children components.
Managing the scope at the time of resolving the logger for constructor injection wouldn't work as the scope would not have been applied by the parent (component A) at that time.
So the 'resolving' of the actual scope/items to be applied for a given logger call (i.e. LogInformation) would need to be evaluated at the time of the logging, not when the logger instance was resolved/instantiated/injected to the components.
What I've explored so far:
I've investigated what native facilities exist but I'm not finding anything that would yield this type of behavior. I did find something close it seemed via the IExternalScopeProvider and that was not resolvable so I tried LoggerExternalScopeProvider and that too wouldn't resolve. I was using a simple services.AddLogging(..) call and expected (hoped) to be able to access those somehow and use the Push(..) method to add my items.
This seemed like it should have been fairly close to what I needed since it was using AsyncLocal to store the current state and then a push/pop (via dispose) approach but given that I couldn't resolve those types and the resolved ILoggerFactory doesn't allow me to access it either - I found myself blocked in pursuing those avenues.
We do use Serilog to back our logging and I believe something does exist with it that would give me this type of behavior but I don't want to couple to Serilog directly, I want to stick with the native MS logging types and extension methods, etc.
Has anyone else implemented this and if so, how and what did you leverage? I'm evaluating building our own but I'd much rather leverage something 'official' than homebrew our own.
Beta Was this translation helpful? Give feedback.
All reactions