@@ -717,7 +717,9 @@ public FilterAndTransform(string filterAndPayloadSpec, int startIdx, int endIdx,
717
717
if ( eventNameFilter != null )
718
718
eventNameFilterPredicate = ( string eventName ) => eventNameFilter == eventName ;
719
719
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 )
721
723
{
722
724
// The filter given to the DiagnosticSource may not work if users don't is 'IsEnabled' as expected.
723
725
// 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,
727
729
var outputArgs = this . Morph ( evnt . Value ) ;
728
730
var eventName = evnt . Key ;
729
731
writeEvent ( newListener . Name , eventName , outputArgs ) ;
730
- } ) , eventNameFilterPredicate ) ;
732
+ }
733
+
734
+ var subscription = newListener . Subscribe ( new CallbackObserver < KeyValuePair < string , object ? > > ( OnEventWritten ) , eventNameFilterPredicate ) ;
731
735
_liveSubscriptions = new Subscriptions ( subscription , _liveSubscriptions ) ;
732
736
}
733
737
} ) ) ;
@@ -948,41 +952,55 @@ internal static void CreateActivityListener(DiagnosticSourceEventSource eventSou
948
952
return false ;
949
953
} ;
950
954
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 )
952
974
{
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 ) )
955
978
{
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 ;
965
981
}
966
- } ;
967
982
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 )
969
993
{
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 ) )
972
997
{
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 ;
982
1000
}
983
- } ;
984
1001
985
- ActivitySource . AddActivityListener ( eventSource . _activityListener ) ;
1002
+ list = list . Next ;
1003
+ }
986
1004
}
987
1005
988
1006
// Move all wildcard nodes at the end of the list.
@@ -1067,6 +1085,7 @@ private void Dispose()
1067
1085
}
1068
1086
}
1069
1087
1088
+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
1070
1089
public List < KeyValuePair < string , string ? > > Morph ( object ? args )
1071
1090
{
1072
1091
// Transform the args into a bag of key-value strings.
@@ -1105,7 +1124,11 @@ private void Dispose()
1105
1124
Interlocked . CompareExchange ( ref _implicitTransformsTable ,
1106
1125
new ConcurrentDictionary < Type , TransformSpec ? > ( 1 , 8 ) , null ) ;
1107
1126
}
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 ) ;
1109
1132
}
1110
1133
1111
1134
// implicitTransformas now fetched from cache or constructed, use it to Fetch all the implicit fields.
@@ -1145,6 +1168,7 @@ private void Dispose()
1145
1168
1146
1169
// Given a type generate all the implicit transforms for type (that is for every field
1147
1170
// generate the spec that fetches it).
1171
+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
1148
1172
private static TransformSpec ? MakeImplicitTransforms ( Type type )
1149
1173
{
1150
1174
TransformSpec ? newSerializableArgs = null ;
@@ -1239,6 +1263,7 @@ public TransformSpec(string transformSpec, int startIdx, int endIdx, TransformSp
1239
1263
/// if the spec is OUTSTR=EVENT_VALUE.PROP1.PROP2.PROP3 and the ultimate value of PROP3 is
1240
1264
/// 10 then the return key value pair is KeyValuePair("OUTSTR","10")
1241
1265
/// </summary>
1266
+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
1242
1267
public KeyValuePair < string , string ? > Morph ( object ? obj )
1243
1268
{
1244
1269
for ( PropertySpec ? cur = _fetches ; cur != null ; cur = cur . Next )
@@ -1289,6 +1314,7 @@ public PropertySpec(string propertyName, PropertySpec? next)
1289
1314
/// Given an object fetch the property that this PropertySpec represents.
1290
1315
/// obj may be null when IsStatic is true, otherwise it must be non-null.
1291
1316
/// </summary>
1317
+ [ RequiresUnreferencedCode ( DiagnosticSource . WriteRequiresUnreferencedCode ) ]
1292
1318
public object ? Fetch ( object ? obj )
1293
1319
{
1294
1320
PropertyFetch ? fetch = _fetchForExpectedType ;
@@ -1331,9 +1357,7 @@ public PropertyFetch(Type? type)
1331
1357
/// <summary>
1332
1358
/// Create a property fetcher for a propertyName
1333
1359
/// </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 ) ]
1337
1361
public static PropertyFetch FetcherForProperty ( Type ? type , string propertyName )
1338
1362
{
1339
1363
if ( propertyName == null )
@@ -1385,7 +1409,7 @@ public static PropertyFetch FetcherForProperty(Type? type, string propertyName)
1385
1409
PropertyInfo ? propertyInfo = typeInfo . GetDeclaredProperty ( propertyName ) ;
1386
1410
if ( propertyInfo == null )
1387
1411
{
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. ") ;
1389
1413
return new PropertyFetch ( type ) ;
1390
1414
}
1391
1415
// Delegate creation below is incompatible with static properties.
0 commit comments