-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Additional Diagnostics in Dependency Injection #56809
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
Conversation
Log events when a ServiceProvider is created: * How many singleton, scoped, transient services? * Log the list of registrations Fix dotnet#56313
|
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsLog events when a ServiceProvider is created:
Fix #56313 Of the list in #56313, I left out
|
Fix ILLink warnings
src/libraries/Microsoft.Extensions.DependencyInjection/src/DependencyInjectionEventSource.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyInjection/src/DependencyInjectionEventSource.cs
Outdated
Show resolved
Hide resolved
… becomes enabled. This allows for listeners to attach to a process after it is running, and get the DI information.
josalem
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple comments, but the EventSource end of things looks okay to me after a quick once-over.
src/libraries/Microsoft.Extensions.DependencyInjection/src/DependencyInjectionEventSource.cs
Outdated
Show resolved
Hide resolved
| int scopedServices = 0; | ||
| int transientServices = 0; | ||
|
|
||
| StringBuilder descriptorBuilder = new StringBuilder("{ \"descriptors\":[ "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally speaking, the pattern is to do less string formatting and to do more writing of events with payloads that can then be processed out-of-proc. This looks like it might be a place where building up the state in-proc is preferred, or that you have to string-ify things anyway, but just wanted to call that out. Definitely good that this is under its own keyword.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, here we want to dump a bunch of data - the list of registered DI services. We are doing something similar above already:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/DependencyInjectionEventSource.cs
Lines 80 to 94 in a1aeb13
| public void CallSiteBuilt(Type serviceType, ServiceCallSite callSite) | |
| { | |
| if (IsEnabled(EventLevel.Verbose, EventKeywords.All)) | |
| { | |
| string format = CallSiteJsonFormatter.Instance.Format(callSite); | |
| int chunkCount = format.Length / MaxChunkSize + (format.Length % MaxChunkSize > 0 ? 1 : 0); | |
| for (int i = 0; i < chunkCount; i++) | |
| { | |
| CallSiteBuilt( | |
| serviceType.ToString(), | |
| format.Substring(i * MaxChunkSize, Math.Min(MaxChunkSize, format.Length - i * MaxChunkSize)), i, chunkCount); | |
| } | |
| } | |
| } |
| // Event source doesn't support large payloads so we chunk formatted call site tree | ||
| public static class Keywords | ||
| { | ||
| public const EventKeywords ServiceProviderInitialized = (EventKeywords)0x1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you might need to specify a keyword for the other events in this provider now. If you turn on keyword 0x1, you'll just get the ServiceProviderInitialized events. If you specify keyword 0, I'm not sure you'll get the others - worth checking via ETW.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you turn on keyword 0x1, you'll just get the ServiceProviderInitialized events.
That was my intention. The existing events all check for EventKeywords.All:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/DependencyInjectionEventSource.cs
Lines 80 to 82 in a1aeb13
| public void CallSiteBuilt(Type serviceType, ServiceCallSite callSite) | |
| { | |
| if (IsEnabled(EventLevel.Verbose, EventKeywords.All)) |
So if you don't specify All, you won't get those events.
If you specify keyword 0, I'm not sure you'll get the others - worth checking via ETW.
When I do
dotnet trace collect --providers Microsoft-Extensions-DependencyInjection:0:Verbose --name OrchardCoreTest
I don't get any events logged.
When I do
dotnet trace collect --providers Microsoft-Extensions-DependencyInjection::Verbose --name OrchardCoreTest
I get them all.
And when I do
dotnet trace collect --providers Microsoft-Extensions-DependencyInjection:1:Verbose --name OrchardCoreTest
I just get the 2 new ones.
…pendencyInjectionEventSource.
|
@davidfowl @maryamariyan - any other feedback here? I think this is ready to merge. |
|
|
Log events when a ServiceProvider is created:
Fix #56313
Of the list in #56313, I left out
How many container instances are in the processes?because this can be determined from the number ofServiceProviderBuiltevents that have occurred in the process.