コード例 #1
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 checkIntervalMilliseconds = 10000)
        {
            checkInterval = TimeSpan.FromMilliseconds(checkIntervalMilliseconds);
            logDirectory  = logDirectoryProvider.Directory;
            Directory.CreateDirectory(logDirectory); // In case Elite Dangerous was not launched yet
            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.TotalMilliseconds; // sometimes the filesystem event does not trigger
            logFlushTimer.Elapsed  += LogFlushTimer_Event;
            logFlushTimer.Enabled   = true;

            currentFile  = JournalFileEnumerator.GetLogFiles(logDirectory).FirstOrDefault();
            filePosition = string.IsNullOrEmpty(currentFile) ? 0 : new FileInfo(currentFile).Length;

            SendEventsFromJournal(false);
            fileWatcher.EnableRaisingEvents = true;
            Log.Info("Started monitoring {directory}", logDirectory);
        }
コード例 #2
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 = JournalFileEnumerator.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();
                }
            }
        }