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

Support ILogger.BeginScope #95

Open
BrianVallelunga opened this issue Jan 31, 2020 · 1 comment
Open

Support ILogger.BeginScope #95

BrianVallelunga opened this issue Jan 31, 2020 · 1 comment

Comments

@BrianVallelunga
Copy link

Looking at the file: https://github.com/stackify/stackify-api-dotnet/blob/master/Src/StackifyLib.CoreLogger/StackifyLogger.cs I see that BeginScope simply returns null. It would be great if the code added the state as metadata to the logs.

For example, if TState was a dictionary, then the logger should include each name/value pair as metadata. If the state was another object, it could serialize the object as well. I care less about having a true hierarchy of states (though that would be nice) than I do about having the metadata added and available in Stackify logs for searching.

Thanks.

@p10tyr
Copy link

p10tyr commented Nov 29, 2021

Your logger provider can implement ISupportExternalScope
which you need to then implement SetScopeProvider in the provider.
And you can either make your own ScopeProvider then or extend Microsofts one to do what you want by newing up LoggerExternalScopeProvider in the LoggerExternalScopeProvider

Yea its a bit gobbly gook but its actually separated out really nicely.. just not documented anywhere. We spent quite some time working this out by looking at the source code in MS.

So any way there are a few things you can do now really.. In the implementation of Log in your Logger you can access more information in the ScopeProvider. For example we loop the scope to avoid wrapping BeginScope in usings.. We just use it as tracking steps so we loop over the scopes and dump them into our logs.

We also created like our own IFormatScopes interface which we can add on our objects and when we loop the scopes we do some type checking in a switch, if we have this interface then we call the GetScopes method on it and its in the format that we want. You could also have that implement the actual log data if you wanted.

From here its all really in your control.. Just make sure its really fast to work with these scopes .. if you find your self needing to use async.. well thats wrong you cant do that.. These need to be rapid because in production you WILL have this hitting thousands of times a second if not more.

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

No branches or pull requests

2 participants