Best Vstest code snippet using Microsoft.TestPlatform.Extensions.EventLogCollector.EventLogDataCollector.Initialize
EventLogDataCollectorTests.cs
Source:EventLogDataCollectorTests.cs
...36 this.mockDataCollectionLogger = new Mock<DataCollectionLogger>();37 this.eventLogDataCollector = new EventLogDataCollector(this.mockFileHelper.Object);38 }39 [TestMethod]40 public void InitializeShouldThrowExceptionIfEventsIsNull()41 {42 Assert.ThrowsException<ArgumentNullException>(43 () =>44 {45 this.eventLogDataCollector.Initialize(46 null,47 null,48 this.mockDataCollectionSink,49 this.mockDataCollectionLogger.Object,50 this.dataCollectionEnvironmentContext);51 });52 }53 [TestMethod]54 public void InitializeShouldThrowExceptionIfCollectionSinkIsNull()55 {56 Assert.ThrowsException<ArgumentNullException>(57 () =>58 {59 this.eventLogDataCollector.Initialize(60 null,61 this.mockDataCollectionEvents.Object,62 null,63 this.mockDataCollectionLogger.Object,64 this.dataCollectionEnvironmentContext);65 });66 }67 [TestMethod]68 public void InitializeShouldThrowExceptionIfLoggerIsNull()69 {70 Assert.ThrowsException<ArgumentNullException>(71 () =>72 {73 this.eventLogDataCollector.Initialize(74 null,75 this.mockDataCollectionEvents.Object,76 this.mockDataCollectionSink,77 null,78 this.dataCollectionEnvironmentContext);79 });80 }81 [TestMethod]82 public void InitializeShouldInitializeDefaultEventLogNames()83 {84 List<string> eventLogNames = new List<string>();85 eventLogNames.Add("System");86 eventLogNames.Add("Security");87 eventLogNames.Add("Application");88 this.eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);89 CollectionAssert.AreEqual(eventLogNames, this.eventLogDataCollector.EventLogNames.ToList());90 }91 [TestMethod]92 public void InitializeShouldInitializeCustomEventLogNamesIfSpecifiedInConfiguration()93 {94 string configurationString =95 @"<Configuration><Setting name=""EventLogs"" value=""MyEventName,MyEventName2"" /></Configuration>";96 List<string> eventLogNames = new List<string>();97 eventLogNames.Add("MyEventName");98 eventLogNames.Add("MyEventName2");99 XmlDocument expectedXmlDoc = new XmlDocument();100 expectedXmlDoc.LoadXml(configurationString);101 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);102 CollectionAssert.AreEqual(eventLogNames, this.eventLogDataCollector.EventLogNames.ToList());103 }104 [TestMethod]105 public void InitializeShouldInitializeDefaultLogEntryTypes()106 {107 List<EventLogEntryType> entryTypes = new List<EventLogEntryType>();108 entryTypes.Add(EventLogEntryType.Error);109 entryTypes.Add(EventLogEntryType.Warning);110 entryTypes.Add(EventLogEntryType.FailureAudit);111 this.eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);112 CollectionAssert.AreEqual(entryTypes, this.eventLogDataCollector.EntryTypes.ToList());113 }114 [TestMethod]115 public void InitializeShouldInitializeEntryTypesIfSpecifiedInConfiguration()116 {117 string configurationString =118 @"<Configuration><Setting name=""EntryTypes"" value=""Error"" /></Configuration>";119 List<EventLogEntryType> entryTypes = new List<EventLogEntryType>();120 entryTypes.Add(EventLogEntryType.Error);121 XmlDocument expectedXmlDoc = new XmlDocument();122 expectedXmlDoc.LoadXml(configurationString);123 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);124 CollectionAssert.AreEqual(entryTypes, this.eventLogDataCollector.EntryTypes.ToList());125 }126 [TestMethod]127 public void InitializeShouldInitializeEventSourcesIfSpecifiedInConfiguration()128 {129 string configurationString =130 @"<Configuration><Setting name=""EventSources"" value=""MyEventSource"" /></Configuration>";131 List<string> eventSources = new List<string>();132 eventSources.Add("MyEventSource");133 XmlDocument expectedXmlDoc = new XmlDocument();134 expectedXmlDoc.LoadXml(configurationString);135 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);136 CollectionAssert.AreEqual(eventSources, this.eventLogDataCollector.EventSources.ToList());137 }138 [TestMethod]139 public void InitializeShouldNotInitializeEventSourcesByDefault()140 {141 this.eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);142 Assert.IsNull(this.eventLogDataCollector.EventSources);143 }144 [TestMethod]145 public void InitializeShouldInitializeMaxEntriesIfSpecifiedInConfiguration()146 {147 string configurationString =148 @"<Configuration><Setting name=""MaxEventLogEntriesToCollect"" value=""20"" /></Configuration>";149 XmlDocument expectedXmlDoc = new XmlDocument();150 expectedXmlDoc.LoadXml(configurationString);151 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);152 Assert.AreEqual(20, this.eventLogDataCollector.MaxEntries);153 }154 [TestMethod]155 public void InitializeShouldSetDefaultMaxEntries()156 {157 this.eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);158 Assert.AreEqual(50000, this.eventLogDataCollector.MaxEntries);159 }160 [TestMethod]161 public void InitializeShouldSubscribeToDataCollectionEvents()162 {163 var testableDataCollectionEvents = new TestableDataCollectionEvents();164 this.eventLogDataCollector.Initialize(null, testableDataCollectionEvents, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);165 Assert.AreEqual(1, testableDataCollectionEvents.GetTestCaseStartInvocationList().Length);166 Assert.AreEqual(1, testableDataCollectionEvents.GetTestCaseEndInvocationList().Length);167 Assert.AreEqual(1, testableDataCollectionEvents.GetTestSessionEndInvocationList().Length);168 Assert.AreEqual(1, testableDataCollectionEvents.GetTestSessionStartInvocationList().Length);169 }170 [TestMethod]171 public void TestSessionStartEventShouldCreateEventLogContainer()172 {173 var eventLogDataCollector = new EventLogDataCollector();174 Assert.AreEqual(eventLogDataCollector.ContextMap.Count, 0);175 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);176 this.mockDataCollectionEvents.Raise(x => x.SessionStart += null, new SessionStartEventArgs());177 Assert.AreEqual(eventLogDataCollector.ContextMap.Count, 1);178 }179 [TestMethod]180 public void TestCaseStartEventShouldCreateEventLogContainer()181 {182 var eventLogDataCollector = new EventLogDataCollector();183 Assert.AreEqual(eventLogDataCollector.ContextMap.Count, 0);184 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);185 this.mockDataCollectionEvents.Raise(x => x.TestCaseStart += null, new TestCaseStartEventArgs(new DataCollectionContext(new SessionId(Guid.NewGuid()), new TestExecId(Guid.NewGuid())), new TestCase()));186 Assert.AreEqual(eventLogDataCollector.ContextMap.Count, 1);187 }188 [TestMethod]189 public void TestCaseEndEventShouldWriteEventLogEntriesAndSendFile()190 {191 var eventLogDataCollector = new EventLogDataCollector();192 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);193 var tc = new TestCase();194 var context = new DataCollectionContext(new SessionId(Guid.NewGuid()), new TestExecId(Guid.NewGuid()));195 this.mockDataCollectionEvents.Raise(x => x.TestCaseStart += null, new TestCaseStartEventArgs(context, tc));196 this.mockDataCollectionEvents.Raise(x => x.TestCaseEnd += null, new TestCaseEndEventArgs(context, tc, TestOutcome.Passed));197 Assert.IsTrue(this.mockDataCollectionSink.IsSendFileAsyncInvoked);198 }199 public void TestCaseEndEventShouldInvokeSendFileAsync()200 {201 var eventLogDataCollector = new EventLogDataCollector();202 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);203 var tc = new TestCase();204 var context = new DataCollectionContext(new SessionId(Guid.NewGuid()), new TestExecId(Guid.NewGuid()));205 this.mockDataCollectionEvents.Raise(x => x.TestCaseStart += null, new TestCaseStartEventArgs(context, tc));206 this.mockDataCollectionEvents.Raise(x => x.TestCaseEnd += null, new TestCaseEndEventArgs(context, tc, TestOutcome.Passed));207 Assert.IsTrue(this.mockDataCollectionSink.IsSendFileAsyncInvoked);208 }209 [TestMethod]210 public void TestCaseEndEventShouldThrowIfTestCaseStartIsNotInvoked()211 {212 var eventLogDataCollector = new EventLogDataCollector();213 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);214 var tc = new TestCase();215 var context = new DataCollectionContext(new SessionId(Guid.NewGuid()), new TestExecId(Guid.NewGuid()));216 Assert.ThrowsException<EventLogCollectorException>(() =>217 {218 this.mockDataCollectionEvents.Raise(x => x.TestCaseEnd += null, new TestCaseEndEventArgs(context, tc, TestOutcome.Passed));219 });220 }221 public void SessionEndEventShouldThrowIfSessionStartEventtIsNotInvoked()222 {223 var eventLogDataCollector = new EventLogDataCollector();224 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);225 var tc = new TestCase();226 Assert.ThrowsException<EventLogCollectorException>(() =>227 {228 this.mockDataCollectionEvents.Raise(x => x.SessionEnd += null, new SessionEndEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));229 });230 }231 [TestMethod]232 public void TestSessionEndEventShouldWriteEventLogEntriesAndSendFile()233 {234 var eventLogDataCollector = new EventLogDataCollector();235 eventLogDataCollector.Initialize(null, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);236 var testcase = new TestCase() { Id = Guid.NewGuid() };237 this.mockDataCollectionEvents.Raise(x => x.SessionStart += null, new SessionStartEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));238 this.mockDataCollectionEvents.Raise(x => x.SessionEnd += null, new SessionEndEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));239 Assert.IsTrue(this.mockDataCollectionSink.IsSendFileAsyncInvoked);240 }241 [TestMethod]242 public void WriteEventLogsShouldCreateXmlFile()243 {244 string configurationString =245 @"<Configuration><Setting name=""MaxEventLogEntriesToCollect"" value=""20"" /><Setting name=""EventLog"" value=""Application"" /><Setting name=""EntryTypes"" value=""Warning"" /></Configuration>";246 XmlDocument expectedXmlDoc = new XmlDocument();247 expectedXmlDoc.LoadXml(configurationString);248 this.mockFileHelper.SetupSequence(x => x.Exists(It.IsAny<string>())).Returns(false).Returns(true);249 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);250 this.eventLogDataCollector.WriteEventLogs(251 new List<EventLogEntry>(),252 20,253 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,254 TimeSpan.MaxValue,255 DateTime.Now);256 this.mockFileHelper.Verify(x => x.WriteAllTextToFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once);257 }258 [TestMethod]259 public void WriteEventLogsShouldThrowExceptionIfThrownByFileHelper()260 {261 string configurationString =262 @"<Configuration><Setting name=""MaxEventLogEntriesToCollect"" value=""20"" /><Setting name=""EventLog"" value=""Application"" /><Setting name=""EntryTypes"" value=""Warning"" /></Configuration>";263 XmlDocument expectedXmlDoc = new XmlDocument();264 expectedXmlDoc.LoadXml(configurationString);265 this.mockFileHelper.Setup(x => x.Exists(It.IsAny<string>())).Throws<Exception>();266 Assert.ThrowsException<Exception>(267 () =>268 {269 this.eventLogDataCollector.WriteEventLogs(270 new List<EventLogEntry>(),271 20,272 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,273 TimeSpan.MaxValue,274 DateTime.Now);275 });276 }277 [TestMethod]278 public void WriteEventLogsShouldFilterTestsBasedOnTimeAndMaxValue()279 {280 string configurationString =281 @"<Configuration><Setting name=""MaxEventLogEntriesToCollect"" value=""20"" /><Setting name=""EventLog"" value=""Application"" /><Setting name=""EntryTypes"" value=""Warning"" /></Configuration>";282 XmlDocument expectedXmlDoc = new XmlDocument();283 expectedXmlDoc.LoadXml(configurationString);284 this.mockFileHelper.SetupSequence(x => x.Exists(It.IsAny<string>())).Returns(false).Returns(true);285 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);286 var entries = new List<EventLogEntry>();287 var eventLog = new EventLog("Application");288 int endIndex = eventLog.Entries[eventLog.Entries.Count - 1].Index;289 int firstIndexInLog = eventLog.Entries[0].Index;290 for (int i = endIndex; i > endIndex - 10; i--)291 {292 entries.Add(eventLog.Entries[i - firstIndexInLog]);293 }294 var filteredEntries = entries.Where(entry => entry.TimeGenerated > DateTime.MinValue && entry.TimeGenerated < DateTime.MaxValue)295 .OrderBy(x => x.TimeGenerated).ToList().Take(5).ToList();296 this.eventLogDataCollector.WriteEventLogs(297 entries,298 5,299 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,300 TimeSpan.MaxValue,301 DateTime.Now);302 this.mockFileHelper.Verify(303 x => x.WriteAllTextToFile(304 It.IsAny<string>(),305 It.Is<string>(306 str => str.Contains(filteredEntries[0].InstanceId.ToString())307 && str.Contains(filteredEntries[1].InstanceId.ToString())308 && str.Contains(filteredEntries[2].InstanceId.ToString())309 && str.Contains(filteredEntries[3].InstanceId.ToString())310 && str.Contains(filteredEntries[4].InstanceId.ToString()))));311 }312 [TestMethod]313 public void WriteEventLogsShouldFilterTestIfMaxValueExceedsEntries()314 {315 string configurationString =316 @"<Configuration><Setting name=""MaxEventLogEntriesToCollect"" value=""20"" /><Setting name=""EventLog"" value=""Application"" /><Setting name=""EntryTypes"" value=""Warning"" /></Configuration>";317 XmlDocument expectedXmlDoc = new XmlDocument();318 expectedXmlDoc.LoadXml(configurationString);319 this.mockFileHelper.SetupSequence(x => x.Exists(It.IsAny<string>())).Returns(false).Returns(true);320 this.eventLogDataCollector.Initialize(expectedXmlDoc.DocumentElement, this.mockDataCollectionEvents.Object, this.mockDataCollectionSink, this.mockDataCollectionLogger.Object, this.dataCollectionEnvironmentContext);321 var entries = new List<EventLogEntry>();322 var eventLog = new EventLog("Application");323 int endIndex = eventLog.Entries[eventLog.Entries.Count - 1].Index;324 int firstIndexInLog = eventLog.Entries[0].Index;325 for (int i = endIndex; i > endIndex - 5; i--)326 {327 entries.Add(eventLog.Entries[i - firstIndexInLog]);328 }329 var filteredEntries = entries.Where(entry => entry.TimeGenerated > DateTime.MinValue && entry.TimeGenerated < DateTime.MaxValue)330 .OrderBy(x => x.TimeGenerated).ToList().Take(10).ToList();331 this.eventLogDataCollector.WriteEventLogs(332 entries,333 5,334 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,...
Initialize
Using AI Code Generation
1using Microsoft.TestPlatform.Extensions.EventLogCollector;2using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 [DataCollectorFriendlyName("EventLogCollector")]10 {11 private EventLogDataCollectorImpl eventLogDataCollectorImpl;12 public override void Initialize(System.Xml.XmlElement configurationElement, System.Xml.XmlElement environmentContext)13 {14 this.eventLogDataCollectorImpl = new EventLogDataCollectorImpl(configurationElement, environmentContext);15 }16 public override void SessionStarted()17 {18 this.eventLogDataCollectorImpl.SessionStarted();19 }20 public override void SessionEnded()21 {22 this.eventLogDataCollectorImpl.SessionEnded();23 }24 public override void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs)25 {26 this.eventLogDataCollectorImpl.TestCaseStarted(testCaseStartEventArgs);27 }28 public override void TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)29 {30 this.eventLogDataCollectorImpl.TestCaseEnded(testCaseEndEventArgs);31 }32 }33}34using Microsoft.TestPlatform.Extensions.EventLogCollector;35using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41{42 {43 private DataCollectionContext dataCollectionContext;44 private DataCollectionEvents events;45 public EventLogDataCollectorImpl(System.Xml.XmlElement configurationElement, System.Xml.XmlElement environmentContext)46 {47 this.dataCollectionContext = new DataCollectionContext(configurationElement, environmentContext);48 }49 public void SessionStarted()50 {51 events = DataCollectionEvents.Instance;52 events.SessionStart += Events_SessionStart;53 events.SessionEnd += Events_SessionEnd;54 events.TestCaseStart += Events_TestCaseStart;55 events.TestCaseEnd += Events_TestCaseEnd;56 }57 public void SessionEnded()58 {59 events.SessionStart -= Events_SessionStart;60 events.SessionEnd -= Events_SessionEnd;61 events.TestCaseStart -= Events_TestCaseStart;62 events.TestCaseEnd -= Events_TestCaseEnd;
Initialize
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.TestPlatform.Extensions.EventLogCollector;7using System.Collections.ObjectModel;8using System.IO;9using System.Xml;10using System.Xml.Linq;11using System.Xml.XPath;12using System.Xml.Xsl;13using System.Xml.Serialization;14using System.Reflection;15using System.Runtime.InteropServices;16using System.Diagnostics;17using System.Xml.Schema;18using System.Xml.Serialization;19using System.Xml.Linq;20{21 {22 static void Main(string[] args)23 {24 EventLogDataCollector eventLogDataCollector = new EventLogDataCollector();25 Dictionary<string, string> eventLogSettings = new Dictionary<string, string>();26 eventLogSettings.Add("LogName", "Application");27 eventLogSettings.Add("SourceName", "Application");28 eventLogSettings.Add("EventLogFileName", "EventLog.xml");29 eventLogDataCollector.Initialize(eventLogSettings);30 Console.ReadLine();31 }32 }33}
Initialize
Using AI Code Generation
1using System;2using Microsoft.TestPlatform.Extensions.EventLogCollector;3{4 {5 public static void Main()6 {7 EventLogDataCollector eventLogDataCollector = new EventLogDataCollector();8 eventLogDataCollector.Initialize(null, null);9 }10 }11}
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!!