Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binlog filepath uniquification option #10051

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eng/dependabot/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Update="Shouldly" Condition="'$(ShouldlyVersion)' != ''" Version="$(ShouldlyVersion)" />

<PackageVersion Include="FakeItEasy" Version="8.1.0" />
<PackageVersion Update="FakeItEasy" Condition="'$(FakeItEasyVersion)' != ''" Version="$(FakeItEasyVersion)" />

<PackageVersion Include="System.CodeDom" Version="8.0.0" />
<PackageVersion Update="System.CodeDom" Condition="'$(SystemCodeDomVersion)' != ''" Version="$(SystemCodeDomVersion)" />

Expand Down
57 changes: 57 additions & 0 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using System.Linq;
using System.Reflection;
using System.Text;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Microsoft.Build.Shared;
using Microsoft.Build.UnitTests.Shared;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -637,6 +639,61 @@ public void SuppressCommandOutputForNonDiagVerbosity()
}
}

[Theory]
// Wildcard - new scenario
[InlineData("mylog-{}-foo", "mylog-xxxxx-foo.binlog")]
[InlineData("mylog-{}-foo-{}", "mylog-xxxxx-foo-xxxxx.binlog")]
[InlineData("\"mylog-{}-foo\"", "mylog-xxxxx-foo.binlog")]
[InlineData("foo\\bar\\mylog-{}-foo.binlog", "foo\\bar\\mylog-xxxxx-foo.binlog")]
[InlineData("ProjectImports=None;LogFile=mylog-{}-foo", "mylog-xxxxx-foo.binlog")]
// No wildcard - pre-existing scenarios
[InlineData("mylog-foo.binlog", "mylog-foo.binlog")]
[InlineData("\"mylog-foo.binlog\"", "mylog-foo.binlog")]
[InlineData("foo\\bar\\mylog-foo.binlog", "foo\\bar\\mylog-foo.binlog")]
[InlineData("ProjectImports=None;LogFile=mylog-foo.binlog", "mylog-foo.binlog")]
public void BinlogFileNameParameterParsing(string parameters, string expectedBinlogFile)
{
var binaryLogger = new BinaryLogger
{
Parameters = parameters
};
string random = "xxxxx";
binaryLogger.PathParameterExpander = _ => random;

var eventSource = A.Fake<IEventSource>();

binaryLogger.Initialize(eventSource);
string expectedLog = Path.GetFullPath(expectedBinlogFile);
binaryLogger.FilePath.Should().BeEquivalentTo(expectedLog);
binaryLogger.Shutdown();
File.Exists(binaryLogger.FilePath).ShouldBeTrue();
FileUtilities.DeleteNoThrow(binaryLogger.FilePath);
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved

// We need to create the file to satisfy the invariant set by the ctor of this testclass
File.Create(_logFile);
}

[Fact]
public void BinlogFileNameWildcardGeneration()
{
var binaryLogger = new BinaryLogger
{
Parameters = "{}"
};

var eventSource = A.Fake<IEventSource>();

binaryLogger.Initialize(eventSource);
binaryLogger.FilePath.Should().EndWith(".binlog");
binaryLogger.FilePath.Length.ShouldBeGreaterThan(10);
binaryLogger.Shutdown();
File.Exists(binaryLogger.FilePath).ShouldBeTrue();
FileUtilities.DeleteNoThrow(binaryLogger.FilePath);

// We need to create the file to satisfy the invariant set by the ctor of this testclass
File.Create(_logFile);
}

public void Dispose()
{
_env.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="FluentAssertions" />
<PackageReference Include="System.Configuration.ConfigurationManager" />
<PackageReference Include="Shouldly" />
<PackageReference Include="FakeItEasy" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="Microsoft.CodeAnalysis.Build.Tasks" />
<PackageReference Include="NuGet.Frameworks">
Expand Down
63 changes: 54 additions & 9 deletions src/Build/Logging/BinaryLogger/BinaryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public enum ProjectImportsCollectionMode
/// </summary>
public ProjectImportsCollectionMode CollectProjectImports { get; set; } = ProjectImportsCollectionMode.Embed;

private string FilePath { get; set; }
internal string FilePath { get; private set; }

/// <summary> Gets or sets the verbosity level.</summary>
/// <remarks>
Expand All @@ -135,6 +135,15 @@ public enum ProjectImportsCollectionMode
/// </summary>
public string Parameters { get; set; }

/// <summary>
/// Optional expander of wildcard(s) within the LogFile path parameter of a binlog <see cref="Parameters"/>.
/// Wildcards can be used in the LogFile parameter in a form for curly brackets ('{}', '{[param]}').
/// Currently, the only supported wildcard is '{}', the optional parameters within the curly brackets
/// are not currently supported, however the string parameter to the <see cref="PathParameterExpander"/> func
/// is reserved for this purpose.
/// </summary>
internal Func<string, string> PathParameterExpander { private get; set; } = ExpandPathParameter;

/// <summary>
/// Initializes the logger by subscribing to events of the specified event source and embedded content source.
/// </summary>
Expand Down Expand Up @@ -417,15 +426,9 @@ private void ProcessParameters(out bool omitInitialInfo)
{
omitInitialInfo = true;
}
else if (parameter.EndsWith(".binlog", StringComparison.OrdinalIgnoreCase))
else if (TryInterpretPathParameter(parameter, out string filePath))
{
FilePath = parameter;
if (FilePath.StartsWith("LogFile=", StringComparison.OrdinalIgnoreCase))
{
FilePath = FilePath.Substring("LogFile=".Length);
}

FilePath = FilePath.Trim('"');
FilePath = filePath;
}
else
{
Expand All @@ -451,5 +454,47 @@ private void ProcessParameters(out bool omitInitialInfo)
throw new LoggerException(message, e, errorCode, helpKeyword);
}
}

private bool TryInterpretPathParameter(string parameter, out string filePath)
{
bool hasPathPrefix = parameter.StartsWith("LogFile=", StringComparison.OrdinalIgnoreCase);

if (hasPathPrefix)
{
parameter = parameter.Substring("LogFile=".Length);
}

parameter = parameter.Trim('"');

bool isWildcard = ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_12) && parameter.Contains("{}");
bool hasProperExtension = parameter.EndsWith(".binlog", StringComparison.OrdinalIgnoreCase);
filePath = parameter;

if (!isWildcard)
{
return hasProperExtension;
}

filePath = parameter.Replace("{}", GetUniqueStamp(), StringComparison.Ordinal);

if (!hasProperExtension)
{
filePath += ".binlog";
}
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

private string GetUniqueStamp()
=> (PathParameterExpander ?? ExpandPathParameter)(string.Empty);

private static string ExpandPathParameter(string parameters)
=> $"{DateTime.UtcNow.ToString("yyyyMMdd-HHmmss")}--{ProcessId}--{StringUtils.GenerateRandomString(6)}";
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved

private static int ProcessId
#if NET
=> Environment.ProcessId;
#else
=> System.Diagnostics.Process.GetCurrentProcess().Id;
#endif
}
}
34 changes: 34 additions & 0 deletions src/Framework/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.Build.Framework;

internal static class StringUtils
{
/// <summary>
/// Generates a random string of the specified length.
/// The generated string is suitable for use in file paths.
/// The randomness distribution is given by the System.Random.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
internal static string GenerateRandomString(int length)
{
// Base64, 2^6 = 64
const int eachStringCharEncodesBites = 6;
const int eachByteHasBits = 8;
const double bytesNumNeededForSingleStringChar = eachStringCharEncodesBites / (double)eachByteHasBits;

int randomBytesNeeded = (int)Math.Ceiling(length * bytesNumNeededForSingleStringChar);
Random random = new();

byte[] randomBytes = new byte[randomBytesNeeded];
random.NextBytes(randomBytes);
// Base64: [A-Z], [a-z], [0-9], +, /, =
// We are replacing '/' to get a valid path
string randomBase64String = Convert.ToBase64String(randomBytes).Replace('/', '_');
return randomBase64String.Substring(0, length);
}
}
Loading