Skip to content

Commit c0fb7e5

Browse files
committed
BinlogNamer
1 parent be626f3 commit c0fb7e5

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

SpellingExclusions.dic

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ awaitable
33
Refactorings
44
Infos
55
cref
6+
binlog
7+
Namer
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Composition;
6+
using Microsoft.CodeAnalysis.Host.Mef;
7+
using Microsoft.CodeAnalysis.Options;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.VisualStudio.Composition;
10+
11+
namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace;
12+
13+
[Export(typeof(BinlogNamer)), Shared]
14+
internal sealed class BinlogNamer
15+
{
16+
/// <summary>
17+
/// The suffix to use for the binary log name; incremented each time we have a new build. Should be incremented with <see cref="Interlocked.Increment(ref int)"/>.
18+
/// </summary>
19+
private int _binaryLogNumericSuffix;
20+
21+
/// <summary>
22+
/// A GUID put into all binary log file names, so that way one session doesn't accidentally overwrite the logs from a prior session.
23+
/// </summary>
24+
private readonly Guid _binaryLogGuidSuffix = Guid.NewGuid();
25+
26+
private readonly IGlobalOptionService _globalOptionService;
27+
private readonly ILogger _logger;
28+
29+
[ImportingConstructor]
30+
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
31+
public BinlogNamer(IGlobalOptionService globalOptionService, ILoggerFactory loggerFactory)
32+
{
33+
_globalOptionService = globalOptionService;
34+
_logger = loggerFactory.CreateLogger<BinlogNamer>();
35+
}
36+
37+
internal string? GetMSBuildBinaryLogPath()
38+
{
39+
if (_globalOptionService.GetOption(LanguageServerProjectSystemOptionsStorage.BinaryLogPath) is not string binaryLogDirectory)
40+
return null;
41+
42+
var numericSuffix = Interlocked.Increment(ref _binaryLogNumericSuffix);
43+
var binaryLogPath = Path.Combine(binaryLogDirectory, $"LanguageServerDesignTimeBuild-{_binaryLogGuidSuffix}-{numericSuffix}.binlog");
44+
45+
_logger.LogInformation($"Logging design-time builds to {binaryLogPath}");
46+
47+
return binaryLogPath;
48+
}
49+
}

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/LanguageServerProjectLoader.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace;
2929

3030
internal abstract class LanguageServerProjectLoader
3131
{
32-
/// <summary>
33-
/// The suffix to use for the binary log name; incremented each time we have a new build. Should be incremented with <see cref="Interlocked.Increment(ref int)"/>.
34-
/// </summary>
35-
private int _binaryLogNumericSuffix;
36-
37-
/// <summary>
38-
/// A GUID put into all binary log file names, so that way one session doesn't accidentally overwrite the logs from a prior session.
39-
/// </summary>
40-
private readonly Guid _binaryLogGuidSuffix = Guid.NewGuid();
41-
4232
protected readonly AsyncBatchingWorkQueue<ProjectToLoad> ProjectsToLoadAndReload;
4333

4434
protected readonly ProjectSystemProjectFactory ProjectFactory;
@@ -49,6 +39,7 @@ internal abstract class LanguageServerProjectLoader
4939
protected readonly ILoggerFactory LoggerFactory;
5040
private readonly ILogger _logger;
5141
private readonly ProjectLoadTelemetryReporter _projectLoadTelemetryReporter;
42+
private readonly BinlogNamer _binlogNamer;
5243
private readonly ProjectFileExtensionRegistry _projectFileExtensionRegistry;
5344
protected readonly ImmutableDictionary<string, string> AdditionalProperties;
5445

@@ -69,7 +60,8 @@ protected LanguageServerProjectLoader(
6960
ILoggerFactory loggerFactory,
7061
IAsynchronousOperationListenerProvider listenerProvider,
7162
ProjectLoadTelemetryReporter projectLoadTelemetry,
72-
ServerConfigurationFactory serverConfigurationFactory)
63+
ServerConfigurationFactory serverConfigurationFactory,
64+
BinlogNamer binlogNamer)
7365
{
7466
ProjectFactory = projectFactory;
7567
_targetFrameworkManager = targetFrameworkManager;
@@ -79,7 +71,7 @@ protected LanguageServerProjectLoader(
7971
LoggerFactory = loggerFactory;
8072
_logger = loggerFactory.CreateLogger(nameof(LanguageServerProjectLoader));
8173
_projectLoadTelemetryReporter = projectLoadTelemetry;
82-
74+
_binlogNamer = binlogNamer;
8375
var workspace = projectFactory.Workspace;
8476
_projectFileExtensionRegistry = new ProjectFileExtensionRegistry(workspace.CurrentSolution.Services, new DiagnosticReporter(workspace));
8577
var razorDesignTimePath = serverConfigurationFactory.ServerConfiguration?.RazorDesignTimePath;
@@ -117,7 +109,7 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
117109

118110
// TODO: support configuration switching
119111

120-
var binaryLogPath = GetMSBuildBinaryLogPath();
112+
var binaryLogPath = _binlogNamer.GetMSBuildBinaryLogPath();
121113

122114
await using var buildHostProcessManager = new BuildHostProcessManager(globalMSBuildProperties: AdditionalProperties, binaryLogPath: binaryLogPath, loggerFactory: LoggerFactory);
123115
var toastErrorReporter = new ToastErrorReporter();
@@ -154,19 +146,6 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<Project
154146
}
155147
}
156148

157-
private string? GetMSBuildBinaryLogPath()
158-
{
159-
if (_globalOptionService.GetOption(LanguageServerProjectSystemOptionsStorage.BinaryLogPath) is not string binaryLogDirectory)
160-
return null;
161-
162-
var numericSuffix = Interlocked.Increment(ref _binaryLogNumericSuffix);
163-
var binaryLogPath = Path.Combine(binaryLogDirectory, $"LanguageServerDesignTimeBuild-{_binaryLogGuidSuffix}-{numericSuffix}.binlog");
164-
165-
_logger.LogInformation($"Logging design-time builds to {binaryLogPath}");
166-
167-
return binaryLogPath;
168-
}
169-
170149
/// <returns>True if the project needs a NuGet restore, false otherwise.</returns>
171150
private async Task<bool> LoadOrReloadProjectAsync(ProjectToLoad projectToLoad, ToastErrorReporter toastErrorReporter, BuildHostProcessManager buildHostProcessManager, CancellationToken cancellationToken)
172151
{

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/LanguageServerProjectSystem.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public LanguageServerProjectSystem(
4141
ILoggerFactory loggerFactory,
4242
IAsynchronousOperationListenerProvider listenerProvider,
4343
ProjectLoadTelemetryReporter projectLoadTelemetry,
44-
ServerConfigurationFactory serverConfigurationFactory)
44+
ServerConfigurationFactory serverConfigurationFactory,
45+
BinlogNamer binlogNamer)
4546
: base(
4647
workspaceFactory.ProjectSystemProjectFactory,
4748
workspaceFactory.TargetFrameworkManager,
@@ -51,7 +52,8 @@ public LanguageServerProjectSystem(
5152
loggerFactory,
5253
listenerProvider,
5354
projectLoadTelemetry,
54-
serverConfigurationFactory)
55+
serverConfigurationFactory,
56+
binlogNamer)
5557
{
5658
_logger = loggerFactory.CreateLogger(nameof(LanguageServerProjectSystem));
5759
}

0 commit comments

Comments
 (0)