From 05cf1fed9f893b9958743f26a4f6f2a59a247b73 Mon Sep 17 00:00:00 2001 From: Rob Holt Date: Mon, 6 Jan 2020 15:37:17 -0800 Subject: [PATCH] Fix log file collision --- .../Commands/StartEditorServicesCommand.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs index 22addea79..09d643e3b 100644 --- a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs +++ b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs @@ -256,7 +256,17 @@ private void StartLogging() _loggerUnsubscribers.Add(_logger.Subscribe(hostLogger)); } - string logPath = Path.Combine(GetLogDirPath(), "StartEditorServices.log"); + string logDirPath = GetLogDirPath(); + string logPath = Path.Combine(logDirPath, "StartEditorServices.log"); + + // Temp debugging sessions may try to reuse this same path, + // so we ensure they have a unique path + if (File.Exists(logPath)) + { + int randomInt = new Random().Next(); + logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X")}.log"); + } + var fileLogger = StreamLogger.CreateWithNewFile(logPath); _disposableResources.Add(fileLogger); IDisposable fileLoggerUnsubscriber = _logger.Subscribe(fileLogger);