diff --git a/src/Microsoft.SqlTools.ServiceLayer/Utility/Logger.cs b/src/Microsoft.SqlTools.ServiceLayer/Utility/Logger.cs index 6bdd39bd..ef785177 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Utility/Logger.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Utility/Logger.cs @@ -4,6 +4,7 @@ // using System; +using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Text; @@ -54,9 +55,29 @@ namespace Microsoft.SqlTools.EditorServices.Utility /// Optional. Specifies the minimum log message level to write to the log file. /// public static void Initialize( - string logFilePath = "SqlToolsService.log", + string logFilePath = "sqltools", LogLevel minimumLogLevel = LogLevel.Normal) { + // get a unique number to prevent conflicts of two process launching at the same time + int uniqueId; + try + { + uniqueId = Process.GetCurrentProcess().Id; + } + catch (Exception) + { + // if the pid look up fails for any reason, just use a random number + uniqueId = new Random().Next(1000, 9999); + } + + // make the log path unique + string fullFileName = string.Format( + "{0}_{1,4:D4}{2,2:D2}{3,2:D2}{4,2:D2}{5,2:D2}{6,2:D2}{7}.log", + logFilePath, + DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, + DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, + uniqueId); + if (logWriter != null) { logWriter.Dispose(); @@ -66,7 +87,7 @@ namespace Microsoft.SqlTools.EditorServices.Utility logWriter = new LogWriter( minimumLogLevel, - logFilePath, + fullFileName, true); } diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/LoggerTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/LoggerTests.cs new file mode 100644 index 00000000..c5ea101a --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/LoggerTests.cs @@ -0,0 +1,62 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IO; +using System.Linq; +using Microsoft.SqlTools.EditorServices.Utility; +using Xunit; + +namespace Microsoft.SqlTools.ServiceLayer.Test.ServiceHost +{ + /// + /// Logger test cases + /// + public class LoggerTests + { + + /// + /// Test to verify that the logger initialization is generating a valid file + /// + [Fact] + public void LoggerDefaultFile() + { + // delete any existing log files from the current directory + Directory.GetFiles(Directory.GetCurrentDirectory()) + .Where(fileName + => fileName.Contains("sqltools_") + && fileName.EndsWith(".log", StringComparison.OrdinalIgnoreCase)) + .ToList() + .ForEach(fileName => File.Delete(fileName)); + + // initialize the logger + Logger.Initialize( + logFilePath: Path.Combine(Directory.GetCurrentDirectory(), "sqltools"), + minimumLogLevel: LogLevel.Verbose); + + // close the logger + Logger.Close(); + + // find the name of the new log file + string logFileName = Directory.GetFiles(Directory.GetCurrentDirectory()) + .Where(fileName + => fileName.Contains("sqltools_") + && fileName.EndsWith(".log", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); + + // validate the log file was created with desired name + Assert.True(!string.IsNullOrWhiteSpace(logFileName)); + if (!string.IsNullOrWhiteSpace(logFileName)) + { + Assert.True(logFileName.Length > "sqltools_.log".Length); + Assert.True(File.Exists(logFileName)); + + // delete the test log file + if (File.Exists(logFileName)) + { + File.Delete(logFileName); + } + } + } + } +}