@@ -125,10 +125,17 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
125125 . ConvertAll ( static tuple => ( ( Service ) tuple . KeyedService , tuple . Registration ) )
126126 : BuildStandardRegistrationList ( c . ComponentRegistry , elementTypeService ) ;
127127
128- var collectionKind = isAnyKeyQuery ? "any-keyed" : "standard" ;
129- var collectionDetail = isAnyKeyQuery
130- ? elementType . FullName ?? elementType . Name
131- : elementTypeService . ToString ( ) ?? elementTypeService . GetType ( ) . Name ;
128+ string ? collectionKind = null ;
129+ string ? collectionDetail = null ;
130+ if ( AutofacMetrics . MetricsEnabled )
131+ {
132+ // The collection kind and detail are only used in recording metrics.
133+ collectionKind = isAnyKeyQuery ? "any-keyed" : "standard" ;
134+ collectionDetail = isAnyKeyQuery
135+ ? elementType . FullName ?? elementType . Name
136+ : elementTypeService . ToString ( ) ?? elementTypeService . GetType ( ) . Name ;
137+ }
138+
132139 return BuildCollection ( c , factory , registrationTuples , p , collectionKind , collectionDetail ) ;
133140 } ) ;
134141
@@ -243,9 +250,10 @@ private static IList BuildCollection(
243250 Func < int , IList > factory ,
244251 List < ( Service Service , ServiceRegistration Registration ) > registrations ,
245252 IEnumerable < Parameter > parameters ,
246- string collectionKind ,
247- string collectionDetail )
253+ string ? collectionKind ,
254+ string ? collectionDetail )
248255 {
256+ // Collection kind and detail will be null unless metrics are enabled.
249257 var recordMetrics = AutofacMetrics . MetricsEnabled ;
250258 ValueStopwatch instrumentationTimer = default ;
251259 if ( recordMetrics )
@@ -275,7 +283,7 @@ private static IList BuildCollection(
275283 if ( recordMetrics )
276284 {
277285 AutofacMetrics . RecordCollectionBuild (
278- kind : collectionKind ,
286+ kind : collectionKind ! ,
279287 detail : collectionDetail ,
280288 itemCount : registrations . Count ,
281289 elapsed : instrumentationTimer . GetElapsedTime ( ) ) ;
0 commit comments