public async Task SuckAsync(CancellationToken cancellationToken) { DateTime?lastTimestamp = null; while (!cancellationToken.IsCancellationRequested) { var parameters = new ContainerLogsParameters() { ShowStderr = true, ShowStdout = true, Follow = true, Timestamps = true, }; //We don't want to keep getting the same old log entries, so we grab this. if (lastTimestamp != null) { parameters.Since = (lastTimestamp.Value.Add(TimeSpan.FromSeconds(LogSlewSeconds)).ToUnixEpochTime()).ToString(); } try { using (var logStream = await _dockerClient.Containers.GetContainerLogsAsync(DockerContainerNames.Application, parameters, cancellationToken)) using (var streamReader = new LogStreamReader(logStream)) { //Read the log event DockerLogEvent logEvent = await streamReader.ReadAsync(cancellationToken); //When this is null, the stream is to be closed. while (logEvent != null) { //Keep track of this so we don't keep sucking down the same log entries. lastTimestamp = logEvent.TimestampUtc; //Add this to the collector. await _batchCollector.AddAsync(logEvent); logEvent = await streamReader.ReadAsync(cancellationToken); } } _logger.Verbose("Application log stream closed."); } catch (TaskCanceledException) { } catch (Exception ex) { _logger.Verbose("Exception occurred: {Message}", ex.Message); } await Task.Delay(TimeSpan.FromSeconds(RetrySeconds), cancellationToken); } }
public async Task AddAsync(DockerLogEvent logEvent) { bool emit = false; using (await _lock.LockAsync()) { _events.Add(logEvent); if (_events.Count >= EmitBatchMaximumSize) { emit = true; } } _timer.Start(); if (emit) { EmitAsync(); } }