Skip to content
Open
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
7 changes: 7 additions & 0 deletions MSBuildCache.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Loca
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Repack.Tests", "src\Repack.Tests\Microsoft.MSBuildCache.Repack.Tests.csproj", "{3BCB6452-B087-4A03-8418-C79F2715DDE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.AzurePipelines.Tests", "src\AzurePipelines.Tests\Microsoft.MSBuildCache.AzurePipelines.Tests.csproj", "{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -68,6 +70,10 @@ Global
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Debug|x64.Build.0 = Debug|x64
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.ActiveCfg = Release|x64
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.Build.0 = Release|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.ActiveCfg = Debug|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.Build.0 = Debug|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.ActiveCfg = Release|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +86,7 @@ Global
{97357681-C75E-445D-8547-46F312D01CED} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{F6586428-E047-42C8-B0AC-048DF6DFAF18} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{3BCB6452-B087-4A03-8418-C79F2715DDE7} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F1CDA78F-A666-431B-BF44-56DA7DF193BA}
Expand Down
39 changes: 39 additions & 0 deletions src/AzurePipelines.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using BuildXL.Cache.ContentStore.Interfaces.Logging;
using BuildXL.Cache.ContentStore.Interfaces.Tracing;
using BuildXL.Cache.ContentStore.Logging;
using Microsoft.MSBuildCache.AzurePipelines;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.MSBuildCache.Tests;

[TestClass]
public class LoggingTests
{
[TestMethod]
public void CacheContextIsEmbedded()
{
using ILogger logger = new Logger();
Context defaultContext = new(logger);
Context cacheContext1 = new(logger);
string message1 = "Hello world!";
string embedded = PipelineCachingCacheClient.EmbedCacheContext(cacheContext1, message1);
PipelineCachingCacheClient.TryExtractContext(embedded, defaultContext, out Context cacheContext2, out string message2);
Assert.AreEqual(message1, message2);
Assert.AreEqual(cacheContext1.TraceId, cacheContext2.TraceId);
}

[TestMethod]
public void CacheContextIsNotEmbedded()
{
using ILogger logger = new Logger();
Context defaultContext = new(logger);
Context cacheContext1 = new(logger);
string message1 = "Hello world!";
PipelineCachingCacheClient.TryExtractContext(message1, defaultContext, out Context cacheContext2, out string message2);
Assert.AreEqual(message1, message2);
Assert.AreEqual(defaultContext.TraceId, cacheContext2.TraceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Only supports x64 due to the RocksDB dependency -->
<Platform>x64</Platform>
<Platforms>x64</Platforms>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<RootNamespace>Microsoft.MSBuildCache.AzurePipelines.Tests</RootNamespace>
<!-- Suppress "Avoid constant arrays as arguments". UTs have many one-off test data arrays. -->
<NoWarn>$(NoWarn);CA1861</NoWarn>
<!-- Suppress "Nested types should not be visible". UTs need to be "public" for MSTest but aren't actualy public, so nested typed might need to be exposed. -->
<NoWarn>$(NoWarn);CA1034</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="morelinq" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AzurePipelines\Microsoft.MSBuildCache.AzurePipelines.csproj" />
</ItemGroup>

<!-- NuGet creates a single set of PackageReference for both Debug and Release - even though Debug and Release reference different packages. -->
<Target Name="PackageUnreference" AfterTargets="ResolvePackageAssets" Condition="('$(Configuration)' == 'Release') And ('$(TargetFramework)' == 'net472')">
<ItemGroup>
<PackageDependencies Remove="Microsoft.BuildXL.Cache.Interfaces" />
<PackageDependencies Remove="Microsoft.BuildXL.Cache.Libraries" />
</ItemGroup>
<ItemGroup>
<RuntimeCopyLocalItems Remove="@(RuntimeCopyLocalItems)" Condition=" '%(NuGetPackageId)' == 'Microsoft.BuildXL.Cache.Interfaces' "/>
<RuntimeCopyLocalItems Remove="@(RuntimeCopyLocalItems)" Condition=" '%(NuGetPackageId)' == 'Microsoft.BuildXL.Cache.Libraries' "/>
</ItemGroup>
<ItemGroup>
<ResolvedCompileFileDefinitions Remove="@(ResolvedCompileFileDefinitions)" Condition=" '%(NuGetPackageId)' == 'Microsoft.BuildXL.Cache.Interfaces' "/>
<ResolvedCompileFileDefinitions Remove="@(ResolvedCompileFileDefinitions)" Condition=" '%(NuGetPackageId)' == 'Microsoft.BuildXL.Cache.Libraries' "/>
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<Platforms>$(Platform)</Platforms>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.MSBuildCache.AzurePipelines.Tests" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Microsoft.MSBuildCache.Common.csproj" />
</ItemGroup>
Expand Down
27 changes: 16 additions & 11 deletions src/AzurePipelines/PipelineCachingCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public PipelineCachingCacheClient(
_azureDevopsTracer = new CallbackAppTraceSource(
(rawMessage, level) =>
{
TryExtractContext(rawMessage, out Context cacheContext, out string message);
TryExtractContext(rawMessage, RootContext, out Context cacheContext, out string message);
message = $"PipelineCachingCacheClient [{level}]: {message}";
switch (level)
{
Expand Down Expand Up @@ -778,26 +778,31 @@ private string ConvertAbsolutePathToUriPath(string path)
return $"/{path}";
}

private const string EmbeddedCacheContextHeader = "[[CacheContext:";
private static readonly Regex extractCacheContext = new Regex(@"\[\[CacheContext:(.*)\]\](.*)", RegexOptions.Compiled);
private const string EmbeddedCacheContextHeader = "<<<CacheContext:";
private const string EmbeddedCacheContextTail = ">>>";
private static readonly Regex extractCacheContext = new Regex(@$"(.*){EmbeddedCacheContextHeader}(.*){EmbeddedCacheContextTail}(.*)", RegexOptions.Compiled);

private static string EmbedCacheContext(Context cacheContext, string message) =>
$"{EmbeddedCacheContextHeader}{cacheContext.TraceId}]]{message}";
internal static string EmbedCacheContext(Context cacheContext, string message) =>
$"{EmbeddedCacheContextHeader}{cacheContext.TraceId}{EmbeddedCacheContextTail}{message}";

private void TryExtractContext(string both, out Context context, out string message)
internal static void TryExtractContext(string both, Context defaultContext, out Context context, out string message)
{
Match match;
if (both.StartsWith(EmbeddedCacheContextHeader, StringComparison.Ordinal) &&
#if NETFRAMEWORK
if (both.IndexOf(EmbeddedCacheContextHeader, StringComparison.Ordinal) >= 0 &&
#else
if (both.Contains(EmbeddedCacheContextHeader, StringComparison.Ordinal) &&
#endif
(match = extractCacheContext.Match(both)).Success &&
Guid.TryParse(match.Captures[0].Value, out Guid contextGuid))
Guid.TryParse(match.Groups[2].Value, out Guid contextGuid))
{
context = new Context(contextGuid, RootContext.Logger);
message = match.Captures[1].Value;
context = new Context(contextGuid, defaultContext.Logger);
message = match.Groups[1].Value + match.Groups[3].Value;
}
else
{
message = both;
context = RootContext;
context = defaultContext;
}
}
}
2 changes: 1 addition & 1 deletion src/Common/Microsoft.MSBuildCache.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="Microsoft.Build.Prediction" />
<PackageReference Include="Microsoft.BuildXL.Cache.Hashing" />
<PackageReference Include="Microsoft.BuildXL.Cache.Interfaces" />
<PackageReference Include="Microsoft.BuildXL.Cache.Interfaces" />
<PackageReference Include="Microsoft.BuildXL.Cache.Libraries" />
<!-- Microsoft.Extensions.Logging.Abstractions is an undeclared dependency of BuildXL.Cache.Libraries. TODO: Remove once BuildXL.Cache.Libraries fixes this -->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
Expand Down