コード例 #1
0
        //todo: refactor using of poolName
        //todo: exceptions handling + logging
        //todo: add unit test
        /// <summary>
        /// It reads the lastest log file till the end and is triggered again after appearance the new log file.
        /// </summary>
        public void Start()
        {
            _logger.InfoFormat("Started IIS log reading. App name: {0}.", Settings.AppName);
            var currentLogFilePath = _logFilePath; //to ensure the last reading
            var currentCursor      = 0;

            while (true)
            {
                var logRecords = W3CEnumerable.FromFile(currentLogFilePath).ToArray();
                if (!logRecords.Any())
                {
                    Thread.Sleep(Settings.ReadingInterval);
                    continue;
                }
                if (currentCursor == logRecords.Length)
                {
                    lock (_locker)
                    {
                        EnsureLastReading(ref currentLogFilePath, ref currentCursor);
                    }
                    Thread.Sleep(Settings.ReadingInterval);
                    continue;
                }
                var newRecords = logRecords.Skip(currentCursor);
                foreach (var record in newRecords)
                {
                    WriteMetrics(record);
                }
                _logger.InfoFormat("Added {0} records. App name: {1}", logRecords.Length - currentCursor, Settings.AppName);
                currentCursor = logRecords.Length;
                Thread.Sleep(Settings.ReadingInterval);
            }
        }
コード例 #2
0
        private void EnsureLastReading(ref string logPath, ref int cursor)
        {
            var isNewLogFileReady = logPath != null && logPath != _logFilePath;

            if (isNewLogFileReady)
            {
                var logRecords = W3CEnumerable.FromFile(logPath).ToArray();
                var newRecords = logRecords.Skip(cursor);
                foreach (var record in newRecords)
                {
                    WriteMetrics(record);
                }
                _logger.InfoFormat("Switched to new file {0} -> {1}. Got {2} records. App name: {3}.",
                                   logPath, _logFilePath, cursor, Settings.AppName);
                logPath = _logFilePath;
                cursor  = 0;
            }
        }