public void OnEventLogEntryWritten()

in src/DataCollectors/Microsoft.TestPlatform.Extensions.EventLogCollector/EventLogContainer.cs [116:219]


    public void OnEventLogEntryWritten(object source, EntryWrittenEventArgs e)
    {
        while (!LimitReached)
        {
            try
            {
                lock (EventLogEntries)
                {
                    int currentCount = EventLog.Entries.Count;
                    if (currentCount == 0)
                    {
                        break;
                    }

                    int firstIndexInLog = EventLog.Entries[0].Index;
                    int mostRecentIndexInLog = EventLog.Entries[currentCount - 1].Index;

                    if (mostRecentIndexInLog == NextEntryIndexToCollect - 1)
                    {
                        // We've already collected the most recent entry in the log
                        break;
                    }

                    if (mostRecentIndexInLog < NextEntryIndexToCollect - 1)
                    {
                        if (EqtTrace.IsWarningEnabled)
                        {
                            EqtTrace.Warning(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "EventLogDataContainer: OnEventLogEntryWritten: Handling clearing of log (mostRecentIndexInLog < eventLogContainer.NextEntryIndex): firstIndexInLog: {0}:, mostRecentIndexInLog: {1}, NextEntryIndex: {2}",
                                    firstIndexInLog,
                                    mostRecentIndexInLog,
                                    NextEntryIndexToCollect));
                        }

                        // Send warning; event log must have been cleared.
                        _dataCollectionLogger.LogWarning(
                            _dataCollectionContext,
                            string.Format(
                                CultureInfo.InvariantCulture,
                                Resource.EventsLostWarning,
                                EventLog.Log));

                        NextEntryIndexToCollect = 0;
                        firstIndexInLog = 0;
                    }

                    for (;
                         NextEntryIndexToCollect <= mostRecentIndexInLog;
                         NextEntryIndexToCollect++)
                    {
                        int nextEntryIndexInCurrentLog = NextEntryIndexToCollect - firstIndexInLog;
                        EventLogEntry nextEntry = EventLog.Entries[nextEntryIndexInCurrentLog];

                        // If an explicit list of event sources was provided, only report log entries from those sources
                        if (_eventSources != null && _eventSources.Count > 0)
                        {
                            if (!_eventSources.Contains(nextEntry.Source))
                            {
                                continue;
                            }
                        }

                        if (!_entryTypes.Contains(nextEntry.EntryType))
                        {
                            continue;
                        }

                        if (EventLogEntries.Count < _maxLogEntries)
                        {
                            EventLogEntries.Add(nextEntry);

                            if (EqtTrace.IsVerboseEnabled)
                            {
                                EqtTrace.Verbose(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "EventLogDataContainer.OnEventLogEntryWritten() add event with Id {0} from position {1} in the current {2} log",
                                        nextEntry.Index,
                                        nextEntryIndexInCurrentLog,
                                        EventLog.Log));
                            }
                        }
                        else
                        {
                            LimitReached = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                _dataCollectionLogger.LogError(
                    _dataCollectionContext,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resource.EventsLostError,
                        EventLog.Log,
                        exception), exception);
            }
        }
    }