Ejemplo n.º 1
0
        public void InitialRead(string directory, ICentralLogServiceCache cache, int maxLinesToRead = 1000)
        {
            if (_readDone)
            {
                return;
            }

            _current.ContinueWith((task) =>
            {
                var token = _tokenSource.Token;
                if (token.IsCancellationRequested)
                {
                    return;
                }

                var files = Directory.GetFiles(directory);

                // ReSharper disable once LocalizableElement
                Console.WriteLine($"Read simulation files from [{directory}]");
                Parallel.ForEach(files, (file) =>
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    var lines = File.ReadAllLines(file);
                    if (maxLinesToRead != -1)
                    {
                        foreach (var line in lines.Take(maxLinesToRead))
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            cache.AddEntry(new LogEntry(file, line));
                        }
                    }
                    else
                    {
                        foreach (var line in lines)
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            cache.AddEntry(new LogEntry(file, line));
                        }
                    }
                });

                _readDone = true;
            });
        }
Ejemplo n.º 2
0
        async Task WaitForNewEntriesAndWrite()
        {
            var token = _source.Token;

            while (token.IsCancellationRequested == false)
            {
                try
                {
                    var available = await _logEntryChannel.Reader.WaitToReadAsync(token);

                    if (!available) // If false the channel is closed
                    {
                        break;
                    }

                    var newEntry = await _logEntryChannel.Reader.ReadAsync();

                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    //Trace.TraceInformation($"CentralLogService add log entry to cache: [{newEntry.FileName}] - [{newEntry.Lines}]");
                    _cache.AddEntry(newEntry);
                }
                catch (Exception e)
                {
                    Trace.TraceError($"WaitForNewEntriesAndWrite - Exception: {e.Message}");
                }
            }
        }