Skip to content

Commit

Permalink
Support for DisableDiagnosticTracing to suppress TRACE (#22883)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpopMSFT authored Jul 8, 2023
2 parents e05bd35 + 81f75a0 commit f3729d4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,25 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<!-- Remove TRACE when DisableDiagnosticTracing is true -->
<Target Name="_DisableDiagnosticTracing"
Condition="'$(DisableDiagnosticTracing)' == 'true'"
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants"
BeforeTargets="CoreCompile">
<ItemGroup>
<_DefineConstantsWithoutTrace Include="$(DefineConstants)" />
<_DefineConstantsWithoutTrace Remove="TRACE"/>
</ItemGroup>

<PropertyGroup>
<DefineConstants>@(_DefineConstantsWithoutTrace)</DefineConstants>
</PropertyGroup>
</Target>

<!-- Add implicitly defined preprocessor symbols to DefineConstants -->
<Target Name="AddImplicitDefineConstants"
Condition=" '$(DisableImplicitFrameworkDefines)' != 'true' "
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants"
DependsOnTargets="GenerateTargetPlatformDefineConstants;GenerateNETCompatibleDefineConstants;GeneratePlatformCompatibleDefineConstants;_DisableDiagnosticTracing"
BeforeTargets="CoreCompile" >
<PropertyGroup>
<DefineConstants Condition=" '@(_ImplicitDefineConstant)' != '' " >$(DefineConstants);@(_ImplicitDefineConstant)</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,5 +1028,49 @@ public void It_warns_on_nonportable_rids(string targetFramework, string[] rids,
result.Should().NotHaveStdOutContaining("NETSDK1206");
}
}

[Theory]
[InlineData(true, "TRACE DISABLED")]
[InlineData(false, "TRACE ENABLED")]
public void It_can_use_implicitly_defined_compilation_constants(bool disableTracing, string expectedOutput)
{
var testProj = new TestProject()
{
Name = "DisableTracing_" + disableTracing.ToString(),
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
IsExe = true,
};
if (disableTracing == true)
{
testProj.AdditionalProperties["DisableDiagnosticTracing"] = "true";
}

testProj.SourceFiles[$"{testProj.Name}.cs"] = @"
using System;
class Program
{
static void Main(string[] args)
{
#if TRACE
Console.WriteLine(""TRACE ENABLED"");
#endif
#if !TRACE
Console.WriteLine(""TRACE DISABLED"");
#endif
}
}";
var testAsset = _testAssetsManager.CreateTestProject(testProj, identifier: disableTracing.ToString());

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name));
buildCommand
.Execute()
.Should()
.Pass();

var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, $"{testProj.Name}{EnvironmentInfo.ExecutableExtension}"));
runCommand
.Execute()
.Should().HaveStdOut(expectedOutput);
}
}
}

0 comments on commit f3729d4

Please sign in to comment.