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
             filePosition = ReadJournalFromPosition(CurrentFile, filePosition);
             if (checkOtherFiles)
             {
                 var latestFile = LogEnumerator.GetLogFiles(LogDirectory).First();
                 if (latestFile != CurrentFile)
                 {
                     CurrentFile  = latestFile;
                     filePosition = ReadJournalFromPosition(CurrentFile, 0);
                 }
             }
         }
         catch (Exception e)
         {
             logger.Error(e, "Error while reading journal file {0}", CurrentFile);
             filePosition = new FileInfo(CurrentFile).Length; // Skipping the 'poisoned' data
         }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="logDirectoryProvider">Log directory name provider</param>
        /// <param name="checkInterval">Check interval in milliseconds</param>
        public JournalMonitor(ILogDirectoryNameProvider logDirectoryProvider, int checkInterval = 5000)
        {
            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).First();
            filePosition = new FileInfo(CurrentFile).Length;
            SendEventsFromJournal(false);
            fileWatcher.EnableRaisingEvents = true;
            logger.Info("Started monitoring on folder {0}", LogDirectory);
        }