Skip to content

Commit 1499729

Browse files
authored
Metrics switch support in the SDK (#35418)
2 parents 2ba6f8b + 7f3057d commit 1499729

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Assets/TestProjects/KitchenSink/TestApp/TestApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>false</EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>
1515
<DebuggerSupport>true</DebuggerSupport>
1616
<EventSourceSupport>false</EventSourceSupport>
17+
<MetricsSupport>false</MetricsSupport>
1718
<InvariantGlobalization>true</InvariantGlobalization>
1819
<PredefinedCulturesOnly>true</PredefinedCulturesOnly>
1920
<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ Copyright (c) .NET Foundation. All rights reserved.
504504
Value="$(DebuggerSupport)"
505505
Trim="true" />
506506

507+
<RuntimeHostConfigurationOption Include="System.Diagnostics.Metrics.Meter.IsSupported"
508+
Condition="'$(MetricsSupport)' != ''"
509+
Value="$(MetricsSupport)"
510+
Trim="true" />
511+
507512
<RuntimeHostConfigurationOption Include="System.Diagnostics.Tracing.EventSource.IsSupported"
508513
Condition="'$(EventSourceSupport)' != ''"
509514
Value="$(EventSourceSupport)"

src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,50 @@ public void It_can_implicitly_define_predefined_Cultures_only(string targetFrame
896896
}
897897
}
898898

899+
[Theory]
900+
[InlineData("True")]
901+
[InlineData("False")]
902+
[InlineData(null)]
903+
public void It_can_evaluate_metrics_support(string value)
904+
{
905+
var testProj = new TestProject()
906+
{
907+
Name = "CheckMetricsSupport",
908+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
909+
IsExe = true,
910+
};
911+
912+
if (value is not null)
913+
{
914+
testProj.AdditionalProperties["MetricsSupport"] = value;
915+
}
916+
917+
var testAsset = _testAssetsManager.CreateTestProject(testProj, identifier: value);
918+
var buildCommand = new BuildCommand(testAsset);
919+
buildCommand
920+
.Execute()
921+
.Should()
922+
.Pass();
923+
924+
string runtimeConfigName = $"{testProj.Name}.runtimeconfig.json";
925+
var outputDirectory = buildCommand.GetOutputDirectory(testProj.TargetFrameworks);
926+
outputDirectory.Should().HaveFile(runtimeConfigName);
927+
928+
string runtimeConfigFile = Path.Combine(outputDirectory.FullName, runtimeConfigName);
929+
string runtimeConfigContents = File.ReadAllText(runtimeConfigFile);
930+
JObject runtimeConfig = JObject.Parse(runtimeConfigContents);
931+
JToken metricsSupport = runtimeConfig["runtimeOptions"]["configProperties"]["System.Diagnostics.Metrics.Meter.IsSupported"];
932+
933+
if (value is null)
934+
{
935+
metricsSupport.Should().BeNull();
936+
}
937+
else
938+
{
939+
metricsSupport.Value<string>().Should().Be(value);
940+
}
941+
}
942+
899943
[Theory]
900944
[InlineData("netcoreapp2.2", null, false, null, false)]
901945
[InlineData(ToolsetInfo.CurrentTargetFramework, null, true, null, true)]

src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAProjectWithAllFeatures.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void It_publishes_the_project_correctly(string targetFramework, string []
6060
""System.AggressiveAttributeTrimming"": true,
6161
""System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization"": false,
6262
""System.Diagnostics.Debugger.IsSupported"": true,
63+
""System.Diagnostics.Metrics.Meter.IsSupported"": false,
6364
""System.Diagnostics.Tracing.EventSource.IsSupported"": false,
6465
""System.Globalization.Invariant"": true,
6566
""System.Globalization.PredefinedCulturesOnly"": true,
@@ -101,7 +102,7 @@ public void It_publishes_the_project_correctly(string targetFramework, string []
101102
baselineConfigJsonObject["runtimeOptions"]["tfm"] = targetFramework;
102103
baselineConfigJsonObject["runtimeOptions"]["framework"]["version"] =
103104
targetFramework == "net6.0" ? "6.0.0" : "1.1.2";
104-
105+
105106
runtimeConfigJsonObject
106107
.Should()
107108
.BeEquivalentTo(baselineConfigJsonObject);

0 commit comments

Comments
 (0)