private void MoveToPermanentStorage(string sessionId, string sourceFile, string fileName, string blobSasUri) { if (string.IsNullOrWhiteSpace(blobSasUri)) { var sessionDirectory = GetLogsFolderForSession(sessionId); var collectFileName = Path.Combine(sessionDirectory, fileName); AppendToMonitoringLog($"Copying file from temp folders to [{collectFileName}]", true); try { FileSystemHelpers.CopyFile(sourceFile, collectFileName); AppendToMonitoringLog($"Copied file from temp folders to [{collectFileName}]", true); } catch (Exception ex) { Logger.LogCpuMonitoringErrorEvent("Failed while moving file to Permanent FileSystem storage", ex, sessionId); } } else { try { string relativeFilePath = Path.Combine("Monitoring", "Logs", sessionId, fileName); Lease lease = Infrastructure.LeaseManager.TryGetLease(relativeFilePath, blobSasUri); if (lease == null) { // This instance is already running this collector Logger.LogCpuMonitoringVerboseEvent($"Could not get lease to upload the memory dump - {relativeFilePath}", sessionId); } AppendToMonitoringLog($"Copying {fileName} from temp folders to Blob Storage", true); var accessCondition = AccessCondition.GenerateLeaseCondition(lease.Id); var taskToUpload = Task.Run(() => { FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var blockBlob = BlobController.GetBlobForFile(relativeFilePath, blobSasUri); blockBlob.UploadFromStream(fileStream, accessCondition); }); while (!taskToUpload.IsCompleted) { lease.Renew(); Logger.LogCpuMonitoringVerboseEvent($"Renewing lease to the blob file", sessionId); Thread.Sleep(Infrastructure.Settings.LeaseRenewalTime); } lease.Release(); AppendToMonitoringLog($"Copied {fileName} from temp folders to Blob Storage", true); } catch (Exception ex) { Logger.LogCpuMonitoringErrorEvent("Failed copying file to blob storage", ex, sessionId); } } }