@@ -717,7 +717,9 @@ public FilterAndTransform(string filterAndPayloadSpec, int startIdx, int endIdx,
717717 if ( eventNameFilter != null )
718718 eventNameFilterPredicate = ( string eventName ) => eventNameFilter == eventName ;
719719
720- var subscription = newListener . Subscribe ( new CallbackObserver < KeyValuePair < string , object ? > > ( delegate ( KeyValuePair < string , object ? > evnt )
720+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2026:RequiresUnreferencedCode" ,
721+ Justification = "DiagnosticSource.Write is marked with RequiresUnreferencedCode." ) ]
722+ void OnEventWritten ( KeyValuePair < string , object ? > evnt )
721723 {
722724 // The filter given to the DiagnosticSource may not work if users don't is 'IsEnabled' as expected.
723725 // Thus we look for any events that may have snuck through and filter them out before forwarding.
@@ -727,7 +729,9 @@ public FilterAndTransform(string filterAndPayloadSpec, int startIdx, int endIdx,
727729 var outputArgs = this . Morph ( evnt . Value ) ;
728730 var eventName = evnt . Key ;
729731 writeEvent ( newListener . Name , eventName , outputArgs ) ;
730- } ) , eventNameFilterPredicate ) ;
732+ }
733+
734+ var subscription = newListener . Subscribe ( new CallbackObserver < KeyValuePair < string , object ? > > ( OnEventWritten ) , eventNameFilterPredicate ) ;
731735 _liveSubscriptions = new Subscriptions ( subscription , _liveSubscriptions ) ;
732736 }
733737 } ) ) ;
@@ -948,41 +952,55 @@ internal static void CreateActivityListener(DiagnosticSourceEventSource eventSou
948952 return false ;
949953 } ;
950954
951- eventSource . _activityListener . ActivityStarted = activity =>
955+ eventSource . _activityListener . ActivityStarted = activity => OnActivityStarted ( eventSource , activity ) ;
956+
957+ eventSource . _activityListener . ActivityStopped = activity => OnActivityStopped ( eventSource , activity ) ;
958+
959+ ActivitySource . AddActivityListener ( eventSource . _activityListener ) ;
960+ }
961+
962+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicProperties , typeof ( Activity ) ) ]
963+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicProperties , typeof ( ActivityContext ) ) ]
964+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicProperties , typeof ( ActivityEvent ) ) ]
965+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicProperties , typeof ( ActivityLink ) ) ]
966+ [ DynamicDependency ( nameof ( DateTime . Ticks ) , typeof ( DateTime ) ) ]
967+ [ DynamicDependency ( nameof ( TimeSpan . Ticks ) , typeof ( TimeSpan ) ) ]
968+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2026:RequiresUnreferencedCode" ,
969+ Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted." ) ]
970+ private static void OnActivityStarted ( DiagnosticSourceEventSource eventSource , Activity activity )
971+ {
972+ FilterAndTransform ? list = eventSource . _activitySourceSpecs ;
973+ while ( list != null )
952974 {
953- FilterAndTransform ? list = eventSource . _activitySourceSpecs ;
954- while ( list != null )
975+ if ( ( list . Events & ActivityEvents . ActivityStart ) != 0 &&
976+ ( activity . Source . Name == list . SourceName || list . SourceName == "*" ) &&
977+ ( list . ActivityName == null || list . ActivityName == activity . OperationName ) )
955978 {
956- if ( ( list . Events & ActivityEvents . ActivityStart ) != 0 &&
957- ( activity . Source . Name == list . SourceName || list . SourceName == "*" ) &&
958- ( list . ActivityName == null || list . ActivityName == activity . OperationName ) )
959- {
960- eventSource . ActivityStart ( activity . Source . Name , activity . OperationName , list . Morph ( activity ) ) ;
961- return ;
962- }
963-
964- list = list . Next ;
979+ eventSource . ActivityStart ( activity . Source . Name , activity . OperationName , list . Morph ( activity ) ) ;
980+ return ;
965981 }
966- } ;
967982
968- eventSource . _activityListener . ActivityStopped = activity =>
983+ list = list . Next ;
984+ }
985+ }
986+
987+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2026:RequiresUnreferencedCode" ,
988+ Justification = "Activity's properties are being preserved with the DynamicDependencies on OnActivityStarted." ) ]
989+ private static void OnActivityStopped ( DiagnosticSourceEventSource eventSource , Activity activity )
990+ {
991+ FilterAndTransform ? list = eventSource . _activitySourceSpecs ;
992+ while ( list != null )
969993 {
970- FilterAndTransform ? list = eventSource . _activitySourceSpecs ;
971- while ( list != null )
994+ if ( ( list . Events & ActivityEvents . ActivityStop ) != 0 &&
995+ ( activity . Source . Name == list . SourceName || list . SourceName == "*" ) &&
996+ ( list . ActivityName == null || list . ActivityName == activity . OperationName ) )
972997 {
973- if ( ( list . Events & ActivityEvents . ActivityStop ) != 0 &&
974- ( activity . Source . Name == list . SourceName || list . SourceName == "*" ) &&
975- ( list . ActivityName == null || list . ActivityName == activity . OperationName ) )
976- {
977- eventSource . ActivityStop ( activity . Source . Name , activity . OperationName , list . Morph ( activity ) ) ;
978- return ;
979- }
980-
981- list = list . Next ;
998+ eventSource . ActivityStop ( activity . Source . Name , activity . OperationName , list . Morph ( activity ) ) ;
999+ return ;
9821000 }
983- } ;
9841001
985- ActivitySource . AddActivityListener ( eventSource . _activityListener ) ;
1002+ list = list . Next ;
1003+ }
9861004 }
9871005
9881006 // Move all wildcard nodes at the end of the list.
@@ -1067,6 +1085,7 @@ private void Dispose()
10671085 }
10681086 }
10691087
1088+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
10701089 public List < KeyValuePair < string , string ? > > Morph ( object ? args )
10711090 {
10721091 // Transform the args into a bag of key-value strings.
@@ -1105,7 +1124,11 @@ private void Dispose()
11051124 Interlocked . CompareExchange ( ref _implicitTransformsTable ,
11061125 new ConcurrentDictionary < Type , TransformSpec ? > ( 1 , 8 ) , null ) ;
11071126 }
1108- implicitTransforms = _implicitTransformsTable . GetOrAdd ( argType , type => MakeImplicitTransforms ( type ) ) ;
1127+ implicitTransforms = _implicitTransformsTable . GetOrAdd ( argType , type => MakeImplicitTransformsWrapper ( type ) ) ;
1128+
1129+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2026:RequiresUnreferencedCode" ,
1130+ Justification = "The Morph method has RequiresUnreferencedCode, but the trimmer can't see through lamdba calls." ) ]
1131+ static TransformSpec ? MakeImplicitTransformsWrapper ( Type transformType ) => MakeImplicitTransforms ( transformType ) ;
11091132 }
11101133
11111134 // implicitTransformas now fetched from cache or constructed, use it to Fetch all the implicit fields.
@@ -1145,6 +1168,7 @@ private void Dispose()
11451168
11461169 // Given a type generate all the implicit transforms for type (that is for every field
11471170 // generate the spec that fetches it).
1171+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
11481172 private static TransformSpec ? MakeImplicitTransforms ( Type type )
11491173 {
11501174 TransformSpec ? newSerializableArgs = null ;
@@ -1239,6 +1263,7 @@ public TransformSpec(string transformSpec, int startIdx, int endIdx, TransformSp
12391263 /// if the spec is OUTSTR=EVENT_VALUE.PROP1.PROP2.PROP3 and the ultimate value of PROP3 is
12401264 /// 10 then the return key value pair is KeyValuePair("OUTSTR","10")
12411265 /// </summary>
1266+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
12421267 public KeyValuePair < string , string ? > Morph ( object ? obj )
12431268 {
12441269 for ( PropertySpec ? cur = _fetches ; cur != null ; cur = cur . Next )
@@ -1289,6 +1314,7 @@ public PropertySpec(string propertyName, PropertySpec? next)
12891314 /// Given an object fetch the property that this PropertySpec represents.
12901315 /// obj may be null when IsStatic is true, otherwise it must be non-null.
12911316 /// </summary>
1317+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
12921318 public object ? Fetch ( object ? obj )
12931319 {
12941320 PropertyFetch ? fetch = _fetchForExpectedType ;
@@ -1331,9 +1357,7 @@ public PropertyFetch(Type? type)
13311357 /// <summary>
13321358 /// Create a property fetcher for a propertyName
13331359 /// </summary>
1334- [ DynamicDependency ( "#ctor(System.Type)" , typeof ( EnumeratePropertyFetch < > ) ) ]
1335- [ DynamicDependency ( "#ctor(System.Type,System.Reflection.PropertyInfo)" , typeof ( RefTypedFetchProperty < , > ) ) ]
1336- [ DynamicDependency ( "#ctor(System.Type,System.Reflection.PropertyInfo)" , typeof ( ValueTypedFetchProperty < , > ) ) ]
1360+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
13371361 public static PropertyFetch FetcherForProperty ( Type ? type , string propertyName )
13381362 {
13391363 if ( propertyName == null )
@@ -1385,7 +1409,7 @@ public static PropertyFetch FetcherForProperty(Type? type, string propertyName)
13851409 PropertyInfo ? propertyInfo = typeInfo . GetDeclaredProperty ( propertyName ) ;
13861410 if ( propertyInfo == null )
13871411 {
1388- Logger . Message ( $ "Property { propertyName } not found on { type } ") ;
1412+ Logger . Message ( $ "Property { propertyName } not found on { type } . Ensure the name is spelled correctly. If you published the application with PublishTrimmed=true, ensure the property was not trimmed away. ") ;
13891413 return new PropertyFetch ( type ) ;
13901414 }
13911415 // Delegate creation below is incompatible with static properties.
0 commit comments