diff --git a/MSBuildCache.sln b/MSBuildCache.sln index 91f9157..024f297 100644 --- a/MSBuildCache.sln +++ b/MSBuildCache.sln @@ -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 @@ -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 @@ -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} diff --git a/src/AzurePipelines.Tests/LoggingTests.cs b/src/AzurePipelines.Tests/LoggingTests.cs new file mode 100644 index 0000000..4276a38 --- /dev/null +++ b/src/AzurePipelines.Tests/LoggingTests.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj new file mode 100644 index 0000000..897b4ea --- /dev/null +++ b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj @@ -0,0 +1,39 @@ + + + + x64 + x64 + net472;net8.0 + Microsoft.MSBuildCache.AzurePipelines.Tests + + $(NoWarn);CA1861 + + $(NoWarn);CA1034 + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj b/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj index 64e072e..11c6229 100644 --- a/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj +++ b/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj @@ -5,6 +5,9 @@ $(Platform) net472;net8.0 + + + diff --git a/src/AzurePipelines/PipelineCachingCacheClient.cs b/src/AzurePipelines/PipelineCachingCacheClient.cs index 44d1bd6..1af3d67 100644 --- a/src/AzurePipelines/PipelineCachingCacheClient.cs +++ b/src/AzurePipelines/PipelineCachingCacheClient.cs @@ -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) { @@ -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 = "<< - $"{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; } } } diff --git a/src/Common/Microsoft.MSBuildCache.Common.csproj b/src/Common/Microsoft.MSBuildCache.Common.csproj index bd1abbe..7fc36ac 100644 --- a/src/Common/Microsoft.MSBuildCache.Common.csproj +++ b/src/Common/Microsoft.MSBuildCache.Common.csproj @@ -12,7 +12,7 @@ - +