2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using Microsoft . Diagnostics . Tools . Monitor . CollectionRules ;
5
+ using Microsoft . Diagnostics . Tools . Monitor . CollectionRules . Actions ;
5
6
using Microsoft . Diagnostics . Tools . Monitor . CollectionRules . Options ;
6
7
using Microsoft . Diagnostics . Tools . Monitor . CollectionRules . Options . Actions ;
7
8
using Microsoft . Extensions . Logging ;
@@ -36,6 +37,7 @@ internal sealed class TokenContext
36
37
internal sealed class ConfigurationTokenParser
37
38
{
38
39
private readonly ILogger _logger ;
40
+ private readonly ICollectionRuleActionOperations _actionOperations ;
39
41
40
42
public const string SubstitutionPrefix = "$(" ;
41
43
public const string SubstitutionSuffix = ")" ;
@@ -58,17 +60,23 @@ internal sealed class ConfigurationTokenParser
58
60
public static readonly string HostNameReference = CreateTokenReference ( MonitorInfoReference , HostName ) ;
59
61
public static readonly string UnixTimeReference = CreateTokenReference ( MonitorInfoReference , UnixTime ) ;
60
62
61
- public ConfigurationTokenParser ( ILogger logger )
63
+ public ConfigurationTokenParser ( ILogger logger , ICollectionRuleActionOperations actionOperations )
62
64
{
63
65
_logger = logger ;
66
+ _actionOperations = actionOperations ;
64
67
}
65
68
66
69
public object ? SubstituteOptionValues ( CollectionRuleActionOptions actionOptions , TokenContext context )
67
70
{
68
71
var originalSettings = actionOptions . Settings ;
69
72
object ? settings = originalSettings ;
70
73
71
- foreach ( PropertyInfo propertyInfo in GetPropertiesFromSettings ( actionOptions ) )
74
+ if ( ! _actionOperations . TryGetOptionsType ( actionOptions . Type , out Type optionsType ) )
75
+ {
76
+ throw new InvalidOperationException ( string . Format ( CultureInfo . InvariantCulture , Strings . ErrorMessage_UnknownActionType , actionOptions . Type ) ) ;
77
+ }
78
+
79
+ foreach ( PropertyInfo propertyInfo in GetPropertiesFromSettings ( optionsType ) )
72
80
{
73
81
string ? originalPropertyValue = ( string ? ) propertyInfo . GetValue ( settings ) ;
74
82
if ( string . IsNullOrEmpty ( originalPropertyValue ) )
@@ -123,33 +131,11 @@ public bool TryCloneSettings(object? originalSettings, ref object? settings)
123
131
return true ;
124
132
}
125
133
126
- public static IEnumerable < PropertyInfo > GetPropertiesFromSettings ( CollectionRuleActionOptions actionOptions , Predicate < PropertyInfo > ? predicate = null )
134
+ public static IEnumerable < PropertyInfo > GetPropertiesFromSettings ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties ) ] Type type , Predicate < PropertyInfo > ? predicate = null )
127
135
{
128
- object ? settings = actionOptions . Settings ;
129
- return actionOptions . Type switch {
130
- KnownCollectionRuleActions . CollectDump => GetPropertiesFromSettings ( typeof ( CollectDumpOptions ) , predicate ) ,
131
- KnownCollectionRuleActions . CollectExceptions => GetPropertiesFromSettings ( typeof ( CollectExceptionsOptions ) , predicate ) ,
132
- KnownCollectionRuleActions . CollectGCDump => GetPropertiesFromSettings ( typeof ( CollectGCDumpOptions ) , predicate ) ,
133
- KnownCollectionRuleActions . CollectLogs => GetPropertiesFromSettings ( typeof ( CollectLogsOptions ) , predicate ) ,
134
- KnownCollectionRuleActions . CollectStacks => GetPropertiesFromSettings ( typeof ( CollectStacksOptions ) , predicate ) ,
135
- KnownCollectionRuleActions . CollectTrace => GetPropertiesFromSettings ( typeof ( CollectTraceOptions ) , predicate ) ,
136
- KnownCollectionRuleActions . CollectLiveMetrics => GetPropertiesFromSettings ( typeof ( CollectLiveMetricsOptions ) , predicate ) ,
137
- KnownCollectionRuleActions . Execute => GetPropertiesFromSettings ( typeof ( ExecuteOptions ) , predicate ) ,
138
- KnownCollectionRuleActions . LoadProfiler => GetPropertiesFromSettings ( typeof ( LoadProfilerOptions ) , predicate ) ,
139
- KnownCollectionRuleActions . SetEnvironmentVariable => GetPropertiesFromSettings ( typeof ( SetEnvironmentVariableOptions ) , predicate ) ,
140
- KnownCollectionRuleActions . GetEnvironmentVariable => GetPropertiesFromSettings ( typeof ( GetEnvironmentVariableOptions ) , predicate ) ,
141
- _ => throw new ArgumentException ( string . Format (
142
- CultureInfo . InvariantCulture ,
143
- "Unknown action type: {0}" ,
144
- actionOptions . Type ) )
145
- } ;
146
-
147
- static IEnumerable < PropertyInfo > GetPropertiesFromSettings ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties ) ] Type type , Predicate < PropertyInfo > ? predicate = null )
148
- {
149
- return type . GetProperties ( BindingFlags . Public | BindingFlags . Instance )
150
- . Where ( p => p . PropertyType == typeof ( string ) && ( predicate ? . Invoke ( p ) ?? true ) )
151
- . ToArray ( ) ;
152
- }
136
+ return type . GetProperties ( BindingFlags . Public | BindingFlags . Instance )
137
+ . Where ( p => p . PropertyType == typeof ( string ) && ( predicate ? . Invoke ( p ) ?? true ) )
138
+ . ToArray ( ) ;
153
139
}
154
140
155
141
private static string CreateTokenReference ( string category , string token ) =>
0 commit comments