Replies: 1 comment 9 replies
-
For logger providers that implement ISupportExternalScope, this could be done by implementing an IExternalScopeProvider that retrieves an overriding list of scopes from thread-local storage, and telling each provider to use that. An ILogger wrapper would then set the scope list, call the per-provider loggers, and restore the list. Likely you'd need a custom ILoggerFactory too. Not all providers support ISupportExternalScope though. The Microsoft.Extensions.Logging.TraceSource package doesn't, because the underlying TraceSource reads the scopes from Trace.CorrelationManager. If you need to support those too, then I think your ILogger has to save an ExecutionContext that has the correct scopes and call the provider-specific loggers on clones of that. Which even seems easier to implement than the IExternalScopeProvider hack. Replacing the ExecutionContext seems likely to break log enrichment as implemented in this repository, though. |
Beta Was this translation helpful? Give feedback.
-
I would like to have a scope attached to a specific instance of an
ILogger
so whenever anyone logs using that specificILogger
instance, that scoped property (SomeProperty
in the below example) is always present.If someone logs using any different
ILogger
instance, that property should not be present.What is the best way to implement this?
The below example demonstrates it. It is not pretty because:
Example
) needs to implementIDisposable
Beta Was this translation helpful? Give feedback.
All reactions