示例#1
0
        private EventLogSessionContext GetEventLogSessionContext(DataCollectionContext dataCollectionContext)
        {
            EventLogSessionContext eventLogSessionContext;
            bool eventLogContainerFound;

            lock (this.ContextMap)
            {
                eventLogContainerFound = this.ContextMap.TryGetValue(dataCollectionContext, out eventLogSessionContext);
            }

            if (!eventLogContainerFound)
            {
                string msg = string.Format(
                    CultureInfo.InvariantCulture,
                    Resource.ContextNotFoundException,
                    dataCollectionContext.ToString());
                throw new EventLogCollectorException(msg, null);
            }

            return(eventLogSessionContext);
        }
示例#2
0
        /// <summary>
        /// The write event logs.
        /// </summary>
        /// <param name="eventLogEntries">
        /// The event log entries.
        /// </param>
        /// <param name="maxLogEntries">
        /// Max Log Entries.
        /// </param>
        /// <param name="dataCollectionContext">
        /// The data collection context.
        /// </param>
        /// <param name="requestedDuration">
        /// The requested duration.
        /// </param>
        /// <param name="timeRequestReceived">
        /// The time request received.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        internal string WriteEventLogs(List <EventLogEntry> eventLogEntries, int maxLogEntries, DataCollectionContext dataCollectionContext, TimeSpan requestedDuration, DateTime timeRequestReceived)
        {
            // Generate a unique but friendly Directory name in the temp directory
            string eventLogDirName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}-{1}-{2:yyyy}{2:MM}{2:dd}-{2:HH}{2:mm}{2:ss}.{2:fff}",
                "Event Log",
                Environment.MachineName,
                DateTime.Now);

            string eventLogDirPath = Path.Combine(Path.GetTempPath(), eventLogDirName);

            // Create the directory
            this.fileHelper.CreateDirectory(eventLogDirPath);

            string eventLogBasePath    = Path.Combine(eventLogDirPath, EventLogFileName);
            bool   unusedFilenameFound = false;

            string eventLogPath = eventLogBasePath + ".xml";

            if (this.fileHelper.Exists(eventLogPath))
            {
                for (int i = 1; !unusedFilenameFound; i++)
                {
                    eventLogPath = eventLogBasePath + "-" + i.ToString(CultureInfo.InvariantCulture) + ".xml";

                    if (!this.fileHelper.Exists(eventLogPath))
                    {
                        unusedFilenameFound = true;
                    }
                }
            }

            DateTime minDate = DateTime.MinValue;

            // Limit entries to a certain time range if requested
            if (requestedDuration < TimeSpan.MaxValue)
            {
                try
                {
                    minDate = timeRequestReceived - requestedDuration;
                }
                catch (ArgumentOutOfRangeException)
                {
                    minDate = DateTime.MinValue;
                }
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            EventLogXmlWriter.WriteEventLogEntriesToXmlFile(
                eventLogPath,
                eventLogEntries.Where(
                    entry => entry.TimeGenerated > minDate && entry.TimeGenerated < DateTime.MaxValue).OrderBy(x => x.TimeGenerated).ToList().Take(maxLogEntries).ToList(),
                this.fileHelper);

            stopwatch.Stop();

            if (EqtTrace.IsVerboseEnabled)
            {
                EqtTrace.Verbose(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "EventLogDataContainer: Wrote {0} event log entries to file '{1}' in {2} seconds",
                        eventLogEntries.Count,
                        eventLogPath,
                        stopwatch.Elapsed.TotalSeconds.ToString(CultureInfo.InvariantCulture)));
            }

            // Write the event log file
            FileTransferInformation fileTransferInformation =
                new FileTransferInformation(dataCollectionContext, eventLogPath, true, this.fileHelper);

            this.dataSink.SendFileAsync(fileTransferInformation);

            if (EqtTrace.IsVerboseEnabled)
            {
                EqtTrace.Verbose(
                    "EventLogDataContainer: Event log successfully sent for data collection context '{0}'.",
                    dataCollectionContext.ToString());
            }

            return(eventLogPath);
        }