Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionManager.GetDataCollectorsEnabledForRun
DataCollectionManager.cs
Source:DataCollectionManager.cs
...148 {149 return executionEnvironmentVariables;150 }151 // Get settings for each data collector, load and initialize the data collectors.152 var enabledDataCollectorsSettings = this.GetDataCollectorsEnabledForRun(dataCollectionRunSettings);153 if (enabledDataCollectorsSettings == null || enabledDataCollectorsSettings.Count == 0)154 {155 return executionEnvironmentVariables;156 }157 foreach (var dataCollectorSettings in enabledDataCollectorsSettings)158 {159 this.LoadAndInitialize(dataCollectorSettings, settingsXml);160 }161 // Once all data collectors have been initialized, query for environment variables162 bool unloadedAnyCollector;163 var dataCollectorEnvironmentVariables = this.GetEnvironmentVariables(out unloadedAnyCollector);164 foreach (var variable in dataCollectorEnvironmentVariables.Values)165 {166 executionEnvironmentVariables.Add(variable.Name, variable.Value);167 }168 return executionEnvironmentVariables;169 }170 /// <inheritdoc/>171 public void Dispose()172 {173 this.Dispose(true);174 // Use SupressFinalize in case a subclass175 // of this type implements a finalizer.176 GC.SuppressFinalize(this);177 }178 /// <inheritdoc/>179 public Collection<AttachmentSet> SessionEnded(bool isCancelled = false)180 {181 // Return null if datacollection is not enabled.182 if (!this.isDataCollectionEnabled)183 {184 return new Collection<AttachmentSet>();185 }186 if (isCancelled)187 {188 this.attachmentManager.Cancel();189 return new Collection<AttachmentSet>();190 }191 var endEvent = new SessionEndEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext);192 this.SendEvent(endEvent);193 var result = new List<AttachmentSet>();194 try195 {196 result = this.attachmentManager.GetAttachments(endEvent.Context);197 }198 catch (Exception ex)199 {200 if (EqtTrace.IsErrorEnabled)201 {202 EqtTrace.Error("DataCollectionManager.SessionEnded: Failed to get attachments : {0}", ex);203 }204 return new Collection<AttachmentSet>(result);205 }206 if (EqtTrace.IsVerboseEnabled)207 {208 this.LogAttachments(result);209 }210 return new Collection<AttachmentSet>(result);211 }212 /// <inheritdoc/>213 public bool SessionStarted()214 {215 // If datacollectors are not configured or datacollection is not enabled, return false.216 if (!this.isDataCollectionEnabled || this.RunDataCollectors.Count == 0)217 {218 return false;219 }220 this.SendEvent(new SessionStartEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));221 return this.events.AreTestCaseEventsSubscribed();222 }223 /// <inheritdoc/>224 public void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs)225 {226 if (!this.isDataCollectionEnabled)227 {228 return;229 }230 var context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId, new TestExecId(testCaseStartEventArgs.TestCaseId));231 testCaseStartEventArgs.Context = context;232 this.SendEvent(testCaseStartEventArgs);233 }234 /// <inheritdoc/>235 public Collection<AttachmentSet> TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)236 {237 if (!this.isDataCollectionEnabled)238 {239 return new Collection<AttachmentSet>();240 }241 var context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId, new TestExecId(testCaseEndEventArgs.TestCaseId));242 testCaseEndEventArgs.Context = context;243 this.SendEvent(testCaseEndEventArgs);244 List<AttachmentSet> result = null;245 try246 {247 result = this.attachmentManager.GetAttachments(testCaseEndEventArgs.Context);248 }249 catch (Exception ex)250 {251 if (EqtTrace.IsErrorEnabled)252 {253 EqtTrace.Error("DataCollectionManager.TestCaseEnded: Failed to get attachments : {0}", ex);254 }255 return new Collection<AttachmentSet>(result);256 }257 if (EqtTrace.IsVerboseEnabled)258 {259 this.LogAttachments(result);260 }261 return new Collection<AttachmentSet>(result);262 }263 /// <summary>264 /// The dispose.265 /// </summary>266 /// <param name="disposing">267 /// The disposing.268 /// </param>269 protected virtual void Dispose(bool disposing)270 {271 if (!this.disposed)272 {273 if (disposing)274 {275 }276 this.disposed = true;277 }278 }279 #region Load and Initialize DataCollectors280 /// <summary>281 /// Tries to get uri of the data collector corresponding to the friendly name. If no such data collector exists return null.282 /// </summary>283 /// <param name="friendlyName">The friendly Name.</param>284 /// <param name="dataCollectorUri">The data collector Uri.</param>285 /// <returns><see cref="bool"/></returns>286 protected virtual bool TryGetUriFromFriendlyName(string friendlyName, out string dataCollectorUri)287 {288 var extensionManager = this.dataCollectorExtensionManager;289 foreach (var extension in extensionManager.TestExtensions)290 {291 if (string.Compare(friendlyName, extension.Metadata.FriendlyName, StringComparison.OrdinalIgnoreCase) == 0)292 {293 dataCollectorUri = extension.Metadata.ExtensionUri;294 return true;295 }296 }297 dataCollectorUri = null;298 return false;299 }300 /// <summary>301 /// Gets the extension using uri.302 /// </summary>303 /// <param name="extensionUri">304 /// The extension uri.305 /// </param>306 /// <returns>307 /// The <see cref="DataCollector"/>.308 /// </returns>309 protected virtual DataCollector TryGetTestExtension(string extensionUri)310 {311 return this.DataCollectorExtensionManager.TryGetTestExtension(extensionUri).Value;312 }313 /// <summary>314 /// Loads and initializes data collector using data collector settings.315 /// </summary>316 /// <param name="dataCollectorSettings">317 /// The data collector settings.318 /// </param>319 /// <param name="settingsXml"> runsettings Xml</param>320 private void LoadAndInitialize(DataCollectorSettings dataCollectorSettings, string settingsXml)321 {322 DataCollectorInformation dataCollectorInfo;323 DataCollectorConfig dataCollectorConfig;324 try325 {326 // Look up the extension and initialize it if one is found.327 var extensionManager = this.DataCollectorExtensionManager;328 var dataCollectorUri = string.Empty;329 this.TryGetUriFromFriendlyName(dataCollectorSettings.FriendlyName, out dataCollectorUri);330 DataCollector dataCollector = null;331 if (!string.IsNullOrWhiteSpace(dataCollectorUri))332 {333 dataCollector = this.TryGetTestExtension(dataCollectorUri);334 }335 if (dataCollector == null)336 {337 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.DataCollectorNotFound, dataCollectorSettings.FriendlyName));338 return;339 }340 if (this.RunDataCollectors.ContainsKey(dataCollector.GetType()))341 {342 // Collector is already loaded (may be configured twice). Ignore duplicates and return.343 return;344 }345 dataCollectorConfig = new DataCollectorConfig(dataCollector.GetType());346 // Attempt to get the data collector information verifying that all of the required metadata for the collector is available.347 dataCollectorInfo = new DataCollectorInformation(348 dataCollector,349 dataCollectorSettings.Configuration,350 dataCollectorConfig,351 this.dataCollectionEnvironmentContext,352 this.attachmentManager,353 this.events,354 this.messageSink,355 settingsXml);356 }357 catch (Exception ex)358 {359 if (EqtTrace.IsErrorEnabled)360 {361 EqtTrace.Error("DataCollectionManager.LoadAndInitialize: exception while creating data collector {0} : {1}", dataCollectorSettings.FriendlyName, ex);362 }363 // No data collector info, so send the error with no direct association to the collector.364 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.DataCollectorInitializationError, dataCollectorSettings.FriendlyName, ex));365 return;366 }367 try368 {369 dataCollectorInfo.InitializeDataCollector();370 lock (this.RunDataCollectors)371 {372 // Add data collectors to run cache.373 this.RunDataCollectors[dataCollectorConfig.DataCollectorType] = dataCollectorInfo;374 }375 }376 catch (Exception ex)377 {378 if (EqtTrace.IsErrorEnabled)379 {380 EqtTrace.Error("DataCollectionManager.LoadAndInitialize: exception while initializing data collector {0} : {1}", dataCollectorSettings.FriendlyName, ex);381 }382 // Log error.383 dataCollectorInfo.Logger.LogError(this.dataCollectionEnvironmentContext.SessionDataCollectionContext, string.Format(CultureInfo.CurrentCulture, Resources.Resources.DataCollectorInitializationError, dataCollectorConfig.FriendlyName, ex.Message));384 // Dispose datacollector.385 dataCollectorInfo.DisposeDataCollector();386 }387 }388 /// <summary>389 /// Finds data collector enabled for the run in data collection settings.390 /// </summary>391 /// <param name="dataCollectionSettings">data collection settings</param>392 /// <returns>List of enabled data collectors</returns>393 private List<DataCollectorSettings> GetDataCollectorsEnabledForRun(DataCollectionRunSettings dataCollectionSettings)394 {395 var runEnabledDataCollectors = new List<DataCollectorSettings>();396 foreach (var settings in dataCollectionSettings.DataCollectorSettingsList)397 {398 if (settings.IsEnabled)399 {400 if (runEnabledDataCollectors.Any(dcSettings => string.Equals(dcSettings.FriendlyName, settings.FriendlyName, StringComparison.OrdinalIgnoreCase)))401 {402 // If Uri or assembly qualified type name is repeated, consider data collector as duplicate and ignore it.403 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.IgnoredDuplicateConfiguration, settings.FriendlyName));404 continue;405 }406 runEnabledDataCollectors.Add(settings);407 }...
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;8using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;9{10 {11 static void Main(string[] args)12 {13 DataCollectionManager dataCollectionManager = new DataCollectionManager();14 Dictionary<string, DataCollectorSettings> settings = new Dictionary<string, DataCollectorSettings>();15 DataCollectionRunSettings dataCollectionRunSettings = new DataCollectionRunSettings(settings);16 IEnumerable<string> dataCollectorsEnabledForRun = dataCollectionManager.GetDataCollectorsEnabledForRun(dataCollectionRunSettings);17 }18 }19}20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25using Microsoft.VisualStudio.TestPlatform.ObjectModel;26using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;27using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;28{29 {30 static void Main(string[] args)31 {32 DataCollectionManager dataCollectionManager = new DataCollectionManager();33 Dictionary<string, DataCollectorSettings> settings = new Dictionary<string, DataCollectorSettings>();34 DataCollectionRunSettings dataCollectionRunSettings = new DataCollectionRunSettings(settings);35 IEnumerable<string> enabledDataCollectors = dataCollectionManager.GetEnabledDataCollectors(dataCollectionRunSettings);36 }37 }38}39using System;40using System.Collections.Generic;41using System.Linq;42using System.Text;43using System.Threading.Tasks;44using Microsoft.VisualStudio.TestPlatform.ObjectModel;45using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;46using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;47{48 {49 static void Main(string[] args)50 {51 DataCollectionManager dataCollectionManager = new DataCollectionManager();52 Dictionary<string, DataCollectorSettings> settings = new Dictionary<string, DataCollectorSettings>();53 DataCollectionRunSettings dataCollectionRunSettings = new DataCollectionRunSettings(settings);54 IEnumerable<AttachmentSet> testRunAttachments = dataCollectionManager.GetTestRunAttachments(dataCollectionRunSettings);55 }56 }57}
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;8using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;9{10 {11 static void Main(string[] args)12 {13 DataCollectionManager dcm = new DataCollectionManager();14 var dataCollectors = dcm.GetDataCollectorsEnabledForRun(new RunContext(), new TestExecutionContext());15 foreach (var dataCollector in dataCollectors)16 {17 Console.WriteLine(dataCollector);18 }19 }20 }21}
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;7{8 {9 static void Main(string[] args)10 {11 DataCollectionManager dcm = new DataCollectionManager();12 List<DataCollectorSettings> dcs = dcm.GetDataCollectorsEnabledForRun();13 foreach (DataCollectorSettings dc in dcs)14 {15 Console.WriteLine("DataCollectorSettings.FriendlyName : " + dc.FriendlyName);16 Console.WriteLine("DataCollectorSettings.AssemblyQualifiedName : " + dc.AssemblyQualifiedName);17 Console.WriteLine("DataCollectorSettings.Configuration : " + dc.Configuration);18 }19 Console.ReadLine();20 }21 }22}
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;7using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;8{9 {10 static void Main(string[] args)11 {12 DataCollectionManager dataCollectionManager = new DataCollectionManager();13 }14 }15}
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;2using System;3using System.Collections.Generic;4{5 {6 public static void Main(string[] args)7 {8 var dataCollectionManager = new DataCollectionManager();9 var dataCollectors = dataCollectionManager.GetDataCollectorsEnabledForRun();10 foreach (var dataCollector in dataCollectors)11 {12 Console.WriteLine(dataCollector.FriendlyName);13 }14 }15 }16}
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;9using System.Reflection;10using System.IO;11using System.Diagnostics;12{13 {14 static void Main(string[] args)15 {16 string dataCollectorPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.dll");17 string dataCollectorConfigPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.config");18 string dataCollectorSettingsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.settings");19 string dataCollectorLogPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.log");20 string dataCollectorResultPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.result");21 string dataCollectorAttachmentPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.attachment");22 string dataCollectorErrorPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.error");23 string dataCollectorFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector.file");24 string dataCollectorFilePath2 = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector2.file");25 string dataCollectorFilePath3 = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector3.file");26 string dataCollectorFilePath4 = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DataCollector4.file");
GetDataCollectorsEnabledForRun
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.ObjectModel;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;9{10 {11 public static void GetDataCollectorsEnabledForRun()12 {13 DataCollectionManager dataCollectionManager = new DataCollectionManager();14 DataCollectionRunSettings dataCollectionRunSettings = new DataCollectionRunSettings(new DataCollectionSettings(new List<DataCollectorSettings>()));15 DataCollectionContext dataCollectionContext = new DataCollectionContext(new SessionStartEventArgs());16 DataCollectionEvents dataCollectionEvents = new DataCollectionEvents();17 DataCollectionEnvironmentContext dataCollectionEnvironmentContext = new DataCollectionEnvironmentContext(new Dictionary<string, string>());18 DataCollectionLogger dataCollectionLogger = new DataCollectionLogger(new TestLoggerEvents());19 DataCollectionSink dataCollectionSink = new DataCollectionSink();20 DataCollectionParameters dataCollectionParameters = new DataCollectionParameters(dataCollectionRunSettings, dataCollectionContext, dataCollectionEvents, dataCollectionEnvironmentContext, dataCollectionLogger, dataCollectionSink);21 var dataCollectorsEnabledForRun = dataCollectionManager.GetDataCollectorsEnabledForRun(dataCollectionParameters);22 if (dataCollectorsEnabledForRun.Count > 0)23 {24 Console.WriteLine("Data collectors are enabled for the current run");25 }26 {27 Console.WriteLine("Data collectors are not enabled for the current run");28 }29 }30 }31}
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!!