Exemple #1
0
        public IEnumerable <EventBase> ReadAllEvents()
        {
            IEnumerable <EventBase> CreateEnumerable()
            {
                var files =
                    string.IsNullOrWhiteSpace(EventFile)
                        ? from file in _journalDirectoryProvider.FindJournalDirectory().Result.GetFiles("Journal.*.log")
                    orderby file.Name descending
                    select file
                        : new[] { new FileInfo(EventFile), new FileInfo(EventFile) }.AsEnumerable();

                foreach (var file in files.Skip(1))
                {
                    using var fileReader =
                              File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    using var sr = new StreamReader(fileReader);
                    var lines = new List <string>();
                    while (!sr.EndOfStream)
                    {
                        lines.Add(sr.ReadLine() !);
                    }

                    lines.Reverse();
                    foreach (var line in lines)
                    {
                        EventBase?ev = null;
                        try
                        {
                            ev = _eventProvider.ProcessJsonEvent(line).Result;
                        }
                        catch (EventNotImplementedException err)
                        {
                        }

                        if (ev != null)
                        {
                            yield return(ev);
                        }
                    }
                }
            }

            return(new EnumerableStream <EventBase>(CreateEnumerable()));
        }
        private async Task SetJournalDirectory()
        {
            try
            {
                var newJournalDirectory = await _journalDirectoryProvider.FindJournalDirectory();

                if (newJournalDirectory == null || JournalDirectory?.FullName == newJournalDirectory.FullName)
                {
                    return;
                }

                _log.LogInformation("Setting journal directory to {filePath}", newJournalDirectory.FullName);
                JournalDirectory = newJournalDirectory;
            }
            catch (Exception ex)
            {
                _log.LogWarning(ex, "Could not find journal directory");
                throw;
            }
        }