Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionAttachmentManager.Initialize
DataCollectionAttachmentManager.cs
Source:DataCollectionAttachmentManager.cs  
...44        private IFileHelper fileHelper;45        #endregion46        #region Constructor47        /// <summary>48        /// Initializes a new instance of the <see cref="DataCollectionAttachmentManager"/> class.49        /// </summary>50        public DataCollectionAttachmentManager()51            : this(new Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.FileHelper())52        {53        }54        /// <summary>55        /// Initializes a new instance of the <see cref="DataCollectionAttachmentManager"/> class.56        /// </summary>57        /// <param name="fileHelper">File helper instance.</param>58        protected DataCollectionAttachmentManager(IFileHelper fileHelper)59        {60            this.fileHelper = fileHelper;61            this.cancellationTokenSource = new CancellationTokenSource();62            this.attachmentTasks = new Dictionary<DataCollectionContext, List<Task>>();63            this.AttachmentSets = new Dictionary<DataCollectionContext, Dictionary<Uri, AttachmentSet>>();64        }65        #endregion66        #region Properties67        /// <summary>68        /// Gets the session output directory.69        /// </summary>70        internal string SessionOutputDirectory { get; private set; }71        /// <summary>72        /// Gets the attachment sets for the given datacollection context.73        /// </summary>74        internal Dictionary<DataCollectionContext, Dictionary<Uri, AttachmentSet>> AttachmentSets75        {76            get; private set;77        }78        #endregion79        #region public methods80        /// <inheritdoc/>81        public void Initialize(SessionId id, string outputDirectory, IMessageSink messageSink)82        {83            ValidateArg.NotNull(id, nameof(id));84            ValidateArg.NotNull(messageSink, nameof(messageSink));85            this.messageSink = messageSink;86            if (string.IsNullOrEmpty(outputDirectory))87            {88                this.SessionOutputDirectory = Path.Combine(Path.GetTempPath(), DefaultOutputDirectoryName, id.Id.ToString());89            }90            else91            {92                // Create a session specific directory under base output directory.93                var expandedOutputDirectory = Environment.ExpandEnvironmentVariables(outputDirectory);94                var absolutePath = Path.GetFullPath(expandedOutputDirectory);95                this.SessionOutputDirectory = Path.Combine(absolutePath, id.Id.ToString());96            }97            // Create the output directory if it doesn't exist.98            if (!Directory.Exists(this.SessionOutputDirectory))99            {100                Directory.CreateDirectory(this.SessionOutputDirectory);101            }102        }103        /// <inheritdoc/>104        public List<AttachmentSet> GetAttachments(DataCollectionContext dataCollectionContext)105        {106            try107            {108                Task.WhenAll(this.attachmentTasks[dataCollectionContext].ToArray()).Wait();109            }110            catch (Exception ex)111            {112                EqtTrace.Error(ex.Message);113            }114            List<AttachmentSet> attachments = new List<AttachmentSet>();115            Dictionary<Uri, AttachmentSet> uriAttachmentSetMap;116            if (this.AttachmentSets.TryGetValue(dataCollectionContext, out uriAttachmentSetMap))117            {118                attachments = uriAttachmentSetMap.Values.ToList();119                this.attachmentTasks.Remove(dataCollectionContext);120                this.AttachmentSets.Remove(dataCollectionContext);121            }122            return attachments;123        }124        /// <inheritdoc/>125        public void AddAttachment(FileTransferInformation fileTransferInfo, AsyncCompletedEventHandler sendFileCompletedCallback, Uri uri, string friendlyName)126        {127            ValidateArg.NotNull(fileTransferInfo, nameof(fileTransferInfo));128            if (string.IsNullOrEmpty(this.SessionOutputDirectory))129            {130                if (EqtTrace.IsErrorEnabled)131                {132                    EqtTrace.Error(133                        "DataCollectionAttachmentManager.AddAttachment: Initialize not invoked.");134                }135                return;136            }137            if (!this.AttachmentSets.ContainsKey(fileTransferInfo.Context))138            {139                var uriAttachmentSetMap = new Dictionary<Uri, AttachmentSet>();140                this.AttachmentSets.Add(fileTransferInfo.Context, uriAttachmentSetMap);141                this.attachmentTasks.Add(fileTransferInfo.Context, new List<Task>());142            }143            if (!this.AttachmentSets[fileTransferInfo.Context].ContainsKey(uri))144            {145                this.AttachmentSets[fileTransferInfo.Context].Add(uri, new AttachmentSet(uri, friendlyName));146            }147            this.AddNewFileTransfer(fileTransferInfo, sendFileCompletedCallback, uri, friendlyName);...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
