Beispiel #1
0
        private void SendEventsFromJournal(bool checkOtherFiles)
        {
            lock (@lock)
                try
                {
                    // We are not checking file size to make decision about whether
                    // we should read the file. Reason being - the log write operations
                    // are often buffered, so we need to open the file to flush buffers
                    if (currentFile != null)
                    {
                        filePosition = ReadJournalFromPosition(currentFile, filePosition);
                    }

                    if (checkOtherFiles || currentFile == null)
                    {
                        string latestFile = LogEnumerator.GetLogFiles(logDirectory).FirstOrDefault();
                        if (latestFile == currentFile || latestFile == null)
                        {
                            return;
                        }

                        currentFile  = latestFile;
                        filePosition = ReadJournalFromPosition(currentFile, 0);
                    }
                }
                catch (FileNotFoundException e)
                {
                    Log.Error()
                    .Message("Journal file not found")
                    .Exception(e)
                    .Property("journal-file", currentFile)
                    .Write();
                    currentFile = null;
                }
            catch (Exception e)
            {
                Log.Error()
                .Message("Error while reading journal file")
                .Exception(e)
                .Property("journal-file", currentFile)
                .Write();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JournalMonitor"/> class.
        ///
        /// </summary>
        /// <param name="logDirectoryProvider">Log directory name provider</param>
        /// <param name="checkInterval">Check interval in milliseconds</param>
        public JournalMonitor(ILogDirectoryNameProvider logDirectoryProvider, int checkInterval = 10000)
        {
            logDirectory = logDirectoryProvider.Directory;
            fileWatcher  = new FileSystemWatcher(logDirectory);

            fileWatcher.Changed     += FileWatcher_Event;
            fileWatcher.Created     += FileWatcher_Event;
            fileWatcher.NotifyFilter = NotifyFilters.CreationTime |
                                       NotifyFilters.FileName |
                                       NotifyFilters.LastWrite |
                                       NotifyFilters.Size;

            logFlushTimer.AutoReset = true;
            logFlushTimer.Interval  = checkInterval; // sometimes the filesystem event does not trigger
            logFlushTimer.Elapsed  += (o, e) => Task.Factory.StartNew(() => SendEventsFromJournal(false));
            logFlushTimer.Enabled   = true;

            currentFile  = LogEnumerator.GetLogFiles(logDirectory).FirstOrDefault();
            filePosition = String.IsNullOrEmpty(currentFile) ? 0 : new FileInfo(currentFile).Length;
            SendEventsFromJournal(false);
            fileWatcher.EnableRaisingEvents = true;
            Log.Info("Started monitoring");
        }