Skip to content

Commit f3729d4

Browse files
authored
Support for DisableDiagnosticTracing to suppress TRACE (#22883)
2 parents e05bd35 + 81f75a0 commit f3729d4

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,25 @@ Copyright (c) .NET Foundation. All rights reserved.
236236
</ItemGroup>
237237
</Target>
238238

239+
<!-- Remove TRACE when DisableDiagnosticTracing is true -->
240+
<Target Name="_DisableDiagnosticTracing"
241+
Condition="'$(DisableDiagnosticTracing)' == 'true'"
242+
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants"
243+
BeforeTargets="CoreCompile">
244+
<ItemGroup>
245+
<_DefineConstantsWithoutTrace Include="$(DefineConstants)" />
246+
<_DefineConstantsWithoutTrace Remove="TRACE"/>
247+
</ItemGroup>
248+
249+
<PropertyGroup>
250+
<DefineConstants>@(_DefineConstantsWithoutTrace)</DefineConstants>
251+
</PropertyGroup>
252+
</Target>
253+
239254
<!-- Add implicitly defined preprocessor symbols to DefineConstants -->
240255
<Target Name="AddImplicitDefineConstants"
241256
Condition=" '$(DisableImplicitFrameworkDefines)' != 'true' "
242-
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants"
257+
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants;_DisableDiagnosticTracing"
243258
BeforeTargets="CoreCompile" >
244259
<PropertyGroup>
245260
<DefineConstants Condition=" '@(_ImplicitDefineConstant)' != '' " >$(DefineConstants);@(_ImplicitDefineConstant)</DefineConstants>

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,5 +1028,49 @@ public void It_warns_on_nonportable_rids(string targetFramework, string[] rids,
10281028
result.Should().NotHaveStdOutContaining("NETSDK1206");
10291029
}
10301030
}
1031+
1032+
[Theory]
1033+
[InlineData(true, "TRACE DISABLED")]
1034+
[InlineData(false, "TRACE ENABLED")]
1035+
public void It_can_use_implicitly_defined_compilation_constants(bool disableTracing, string expectedOutput)
1036+
{
1037+
var testProj = new TestProject()
1038+
{
1039+
Name = "DisableTracing_" + disableTracing.ToString(),
1040+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
1041+
IsExe = true,
1042+
};
1043+
if (disableTracing == true)
1044+
{
1045+
testProj.AdditionalProperties["DisableDiagnosticTracing"] = "true";
1046+
}
1047+
1048+
testProj.SourceFiles[$"{testProj.Name}.cs"] = @"
1049+
using System;
1050+
class Program
1051+
{
1052+
static void Main(string[] args)
1053+
{
1054+
#if TRACE
1055+
Console.WriteLine(""TRACE ENABLED"");
1056+
#endif
1057+
#if !TRACE
1058+
Console.WriteLine(""TRACE DISABLED"");
1059+
#endif
1060+
}
1061+
}";
1062+
var testAsset = _testAssetsManager.CreateTestProject(testProj, identifier: disableTracing.ToString());
1063+
1064+
var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name));
1065+
buildCommand
1066+
.Execute()
1067+
.Should()
1068+
.Pass();
1069+
1070+
var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, $"{testProj.Name}{EnvironmentInfo.ExecutableExtension}"));
1071+
runCommand
1072+
.Execute()
1073+
.Should().HaveStdOut(expectedOutput);
1074+
}
10311075
}
10321076
}

0 commit comments

Comments
 (0)