private void InitializeLogFile(string logFileIdentifier)
        {
            var logDirectory = _feedbackLogDirectoryProvider.GetDirectory();

            // Ensure a unique file name, in case another log session started around the same time.
            for (var index = 0; ; index++)
            {
                var fileName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1:yyyyMMdd_HHmmss}{2}.log",
                    logFileIdentifier,
                    DateTime.UtcNow,
                    index == 0 ? string.Empty : "." + index);

                var filePath = Path.Combine(logDirectory, fileName);
                try
                {
                    var fileStream = File.Open(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read);

                    _logWriter = new StreamWriter(fileStream);
                    _logFile   = filePath;
                    return;
                }
                catch (IOException)
                {
                    // The file probably already exists. Try again with the next index,
                    // unless there were already too many failures.
                    if (index == 9)
                    {
                        throw;
                    }
                }
            }
        }
Example #2
0
        public IReadOnlyCollection <string> GetFiles()
        {
            if (!_feedbackLogDirectoryProvider.DirectoryCreated)
            {
                // No one requested to create any feedback logs, no reason for us to provide any logs.
                return(Array.Empty <string>());
            }

            var timestamp    = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture);
            var zipFileName  = $"RazorLogs_{timestamp}.zip";
            var zipFilePath  = Path.Combine(Path.GetTempPath(), zipFileName);
            var logDirectory = _feedbackLogDirectoryProvider.GetDirectory();

            if (!Directory.Exists(logDirectory))
            {
                // This should never be the case, just being extra defensive.
                return(Array.Empty <string>());
            }

            _ = _joinableTaskFactory.RunAsync(() => ZipLogsAsync(logDirectory, zipFilePath));

            return(new[] { zipFilePath });
        }