Пример #1
0
        private TimeSpan RunOnce()
        {
            bool performedWork = false;

            KeyValuePair <ILogFile, LogFileSection> pair;

            while (_pendingSections.TryDequeue(out pair))
            {
                var sender       = pair.Key;
                var innerLogFile = _innerLogFile;
                var section      = pair.Value;
                if (sender != innerLogFile)
                {
                    // If, for some reason, we receive an event from a previous log file,
                    // then we ignore it so our listeners are not confused.
                    Log.DebugFormat(
                        "Skipping pending modification '{0}' from '{1}' because it is no longer our current log file '{2}'",
                        section, sender, innerLogFile);
                }
                else
                {
                    if (section.IsReset)
                    {
                        _listeners.Reset();
                    }
                    else if (section.IsInvalidate)
                    {
                        _listeners.Invalidate((int)section.Index, section.Count);
                    }
                    else
                    {
                        _listeners.OnRead((int)(section.Index + section.Count));
                    }
                }

                performedWork = true;
            }

            // This line is extremely important because listeners are allowed to limit how often they are notified.
            // This means that even when there is NO modification to the source, we still need to notify the collection
            // so it can check if enough time has ellapsed to finally notify listener.
            _listeners.OnRead(_listeners.CurrentLineIndex);

            if (performedWork)
            {
                return(TimeSpan.Zero);
            }

            return(TimeSpan.FromMilliseconds(10));
        }
Пример #2
0
        /// <summary>
        ///     Adds a multi line log entry to this log file.
        /// </summary>
        /// <param name="level"></param>
        /// <param name="timestamp"></param>
        /// <param name="lines"></param>
        public void AddMultilineEntry(LevelFlags level, DateTime?timestamp, params string[] lines)
        {
            lock (_syncRoot)
            {
                LogEntryIndex logEntryIndex;
                TimeSpan?     elapsed, deltaTime;
                if (_logEntries.Count > 0)
                {
                    var first = _logEntries[0];
                    var last  = _logEntries[_logEntries.Count - 1];

                    logEntryIndex = last.LogEntryIndex + 1;
                    elapsed       = timestamp - first.Timestamp;
                    deltaTime     = timestamp - last.Timestamp;
                }
                else
                {
                    logEntryIndex = 0;
                    elapsed       = null;
                    deltaTime     = null;

                    _properties.SetValue(LogFileProperties.StartTimestamp, timestamp);
                }
                _properties.SetValue(LogFileProperties.EndTimestamp, timestamp);

                foreach (var line in lines)
                {
                    var logEntry = new LogEntry2();
                    logEntry.Add(LogFileColumns.Index, _logEntries.Count);
                    logEntry.Add(LogFileColumns.OriginalIndex, _logEntries.Count);
                    logEntry.Add(LogFileColumns.LineNumber, _logEntries.Count + 1);
                    logEntry.Add(LogFileColumns.OriginalLineNumber, _logEntries.Count + 1);
                    logEntry.Add(LogFileColumns.LogEntryIndex, logEntryIndex);
                    logEntry.Add(LogFileColumns.RawContent, line);
                    logEntry.Add(LogFileColumns.LogLevel, level);
                    logEntry.Add(LogFileColumns.Timestamp, timestamp);
                    logEntry.Add(LogFileColumns.ElapsedTime, elapsed);
                    logEntry.Add(LogFileColumns.DeltaTime, deltaTime);
                    _logEntries.Add(logEntry);
                    MaxCharactersPerLine = Math.Max(MaxCharactersPerLine, line.Length);
                }
                Touch();
                _listeners.OnRead(_logEntries.Count);
            }
        }