Skip to content

Commit a48071e

Browse files
committed
Add ConfigurationTokenParser, UnitTestsCommon changes
1 parent 15c8706 commit a48071e

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon/Microsoft.Diagnostics.Monitoring.Tool.UnitTestCommon.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>$(ToolTargetFrameworks)</TargetFrameworks>
55
<DisableCompileTimeOpenApiXmlGenerator>true</DisableCompileTimeOpenApiXmlGenerator>
6+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -28,4 +29,15 @@
2829
<InternalsVisibleTo Include="Microsoft.Diagnostics.Monitoring.S3StorageTests.UnitTests" />
2930
</ItemGroup>
3031

32+
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
33+
<KnownFrameworkReference Update="Microsoft.NETCore.App" TargetingPackVersion="$(NetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(NetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(NetCoreAppVersion)" />
34+
<KnownFrameworkReference Update="Microsoft.AspNetCore.App" TargetingPackVersion="$(AspNetCoreAppVersion)" DefaultRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" LatestRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" />
35+
</ItemGroup>
36+
37+
<Target Name="RemoveAnalyzer" BeforeTargets="CoreCompile">
38+
<ItemGroup>
39+
<Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.FileName)' == 'Microsoft.AspNetCore.Http.ValidationsGenerator'" />
40+
</ItemGroup>
41+
</Target>
42+
3143
</Project>

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample/Microsoft.Diagnostics.Monitoring.Tool.UnitTestsSample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
LatestRuntimeFrameworkVersion="$(AspNetCoreAppVersion)" />
3232
</ItemGroup>
3333

34-
<!-- <Target Name="RemoveAnalyzer" BeforeTargets="CoreCompile">
34+
<Target Name="RemoveAnalyzer" BeforeTargets="CoreCompile">
3535
<ItemGroup>
3636
<Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.FileName)' == 'Microsoft.AspNetCore.Http.ValidationsGenerator'" />
3737
</ItemGroup>
38-
</Target> -->
38+
</Target>
3939

4040
</Project>

src/Tools/dotnet-monitor/ConfigurationTokenParser.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules;
5+
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Actions;
56
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options;
67
using Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Options.Actions;
78
using Microsoft.Extensions.Logging;
@@ -36,6 +37,7 @@ internal sealed class TokenContext
3637
internal sealed class ConfigurationTokenParser
3738
{
3839
private readonly ILogger _logger;
40+
private readonly ICollectionRuleActionOperations _actionOperations;
3941

4042
public const string SubstitutionPrefix = "$(";
4143
public const string SubstitutionSuffix = ")";
@@ -58,17 +60,23 @@ internal sealed class ConfigurationTokenParser
5860
public static readonly string HostNameReference = CreateTokenReference(MonitorInfoReference, HostName);
5961
public static readonly string UnixTimeReference = CreateTokenReference(MonitorInfoReference, UnixTime);
6062

61-
public ConfigurationTokenParser(ILogger logger)
63+
public ConfigurationTokenParser(ILogger logger, ICollectionRuleActionOperations actionOperations)
6264
{
6365
_logger = logger;
66+
_actionOperations = actionOperations;
6467
}
6568

6669
public object? SubstituteOptionValues(CollectionRuleActionOptions actionOptions, TokenContext context)
6770
{
6871
var originalSettings = actionOptions.Settings;
6972
object? settings = originalSettings;
7073

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))
7280
{
7381
string? originalPropertyValue = (string?)propertyInfo.GetValue(settings);
7482
if (string.IsNullOrEmpty(originalPropertyValue))
@@ -123,33 +131,11 @@ public bool TryCloneSettings(object? originalSettings, ref object? settings)
123131
return true;
124132
}
125133

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)
127135
{
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();
153139
}
154140

155141
private static string CreateTokenReference(string category, string token) =>

0 commit comments

Comments
 (0)