private void AddNewFileTransfer()

in src/Microsoft.TestPlatform.Common/DataCollection/DataCollectionAttachmentManager.cs [253:352]


    private void AddNewFileTransfer(FileTransferInformation fileTransferInfo, AsyncCompletedEventHandler sendFileCompletedCallback, Uri uri, string friendlyName)
    {
        var context = fileTransferInfo.Context;
        Debug.Assert(
            context != null,
            "DataCollectionManager.AddNewFileTransfer: FileDataHeaderMessage with null context.");

        var testCaseId = fileTransferInfo.Context.HasTestCase
            ? fileTransferInfo.Context.TestExecId.Id.ToString()
            : string.Empty;

        var directoryPath = Path.Combine(
            SessionOutputDirectory,
            testCaseId);
        var localFilePath = Path.Combine(directoryPath, Path.GetFileName(fileTransferInfo.FileName));

        var task = Task.Factory.StartNew(
            () =>
            {
                Validate(fileTransferInfo, localFilePath);

                if (_cancellationTokenSource.Token.IsCancellationRequested)
                {
                    _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                }

                try
                {
                    if (fileTransferInfo.PerformCleanup)
                    {
                        if (EqtTrace.IsInfoEnabled)
                        {
                            EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Moving file {0} to {1}", fileTransferInfo.FileName, localFilePath);
                        }

                        _fileHelper.MoveFile(fileTransferInfo.FileName, localFilePath);

                        if (EqtTrace.IsInfoEnabled)
                        {
                            EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Moved file {0} to {1}", fileTransferInfo.FileName, localFilePath);
                        }
                    }
                    else
                    {
                        if (EqtTrace.IsInfoEnabled)
                        {
                            EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Copying file {0} to {1}", fileTransferInfo.FileName, localFilePath);
                        }

                        _fileHelper.CopyFile(fileTransferInfo.FileName, localFilePath);

                        if (EqtTrace.IsInfoEnabled)
                        {
                            EqtTrace.Info("DataCollectionAttachmentManager.AddNewFileTransfer : Copied file {0} to {1}", fileTransferInfo.FileName, localFilePath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError(
                        ex.ToString(),
                        uri,
                        friendlyName,
                        Guid.Parse(testCaseId));

                    throw;
                }
            },
            _cancellationTokenSource.Token);

        var continuationTask = task.ContinueWith(
            (t) =>
            {
                try
                {
                    if (t.Exception == null)
                    {
                        lock (AttachmentTaskLock)
                        {
                            AttachmentSets[fileTransferInfo.Context][uri].Attachments.Add(UriDataAttachment.CreateFrom(localFilePath, fileTransferInfo.Description));
                        }
                    }

                    sendFileCompletedCallback?.Invoke(this, new AsyncCompletedEventArgs(t.Exception, false, fileTransferInfo.UserToken));
                }
                catch (Exception e)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error(
                            "DataCollectionAttachmentManager.TriggerCallBack: Error occurred while raising the file transfer completed callback for {0}. Error: {1}",
                            localFilePath,
                            e.ToString());
                    }
                }
            },
            _cancellationTokenSource.Token);

        _attachmentTasks[fileTransferInfo.Context].Add(continuationTask);
    }