diff --git a/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs b/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs index 05c5ae53708..0b55b80359a 100644 --- a/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs +++ b/src/Build/BackEnd/Components/Logging/NodeLoggingContext.cs @@ -69,7 +69,13 @@ internal ProjectLoggingContext LogProjectStarted(BuildRequestEntry requestEntry) internal ProjectLoggingContext LogProjectStarted(BuildRequest request, BuildRequestConfiguration configuration) { ErrorUtilities.VerifyThrow(this.IsValid, "Build not started."); - return new ProjectLoggingContext(this, request, configuration.ProjectFullPath, configuration.ToolsVersion, request.ParentBuildEventContext, configuration.Project?.EvaluationId ?? BuildEventContext.InvalidEvaluationId); + + // If we can retrieve the evaluationId from the project, do so. Don't if it's not available or + // if we'd have to retrieve it from the cache in order to access it. + // Order is important here because the Project getter will throw if IsCached. + int evaluationId = (configuration != null && !configuration.IsCached && configuration.Project != null) ? configuration.Project.EvaluationId : BuildEventContext.InvalidEvaluationId; + + return new ProjectLoggingContext(this, request, configuration.ProjectFullPath, configuration.ToolsVersion, request.ParentBuildEventContext, evaluationId); } ///