Esempio n. 1
0
        /// <summary>
        /// Repeatedly make list log entries request till it gets desired number of logs or it reaches end.
        /// _nextPageToken is used to control if it is getting first page or continuous page.
        ///
        /// On complex filters, scanning through logs take time. The server returns empty results
        ///   with a next page token. Continue to send request till some logs are found.
        /// </summary>
        /// <param name="autoReload">Indicate if the request comes from autoReload event.</param>
        private async Task LoadLogsAsync(bool autoReload = false)
        {
            var tokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = tokenSource.Token;

            CancellationTokenSource = tokenSource;

            if (_logs.Count >= MaxLogEntriesCount)
            {
                UserPromptService.Default.ErrorPrompt(
                    message: Resources.LogViewerResultSetTooLargeMessage,
                    title: Resources.UiDefaultPromptTitle);
                CancellationTokenSource?.Cancel();
                return;
            }

            try
            {
                DateTime startTimestamp = DateTime.Now;

                string order = DateTimePickerModel.IsDescendingOrder ? "timestamp desc" : "timestamp asc";
                int    count = 0;
                do
                {
                    // Here, it does not do pageSize: _defaultPageSize - count,
                    // Because this is requried to use same page size for getting next page.
                    LogEntryRequestResult results = await DataSource.ListLogEntriesAsync(
                        _filter, order,
                        pageSize : DefaultPageSize, nextPageToken : _nextPageToken, cancelToken : cancellationToken);

                    _nextPageToken = results.NextPageToken;
                    if (results.LogEntries != null)
                    {
                        count += results.LogEntries.Count;
                        AddLogs(results.LogEntries, autoReload);
                    }

                    if (string.IsNullOrWhiteSpace(_nextPageToken))
                    {
                        _nextPageToken = null;
                    }
                } while (count < DefaultPageSize && !cancellationToken.IsCancellationRequested &&
                         _nextPageToken != null);

                EventsReporterWrapper.ReportEvent(LogsViewerLogsLoadedEvent.Create(CommandStatus.Success, DateTime.Now - startTimestamp));
            }
            catch (Exception)
            {
                _nextPageToken = null;
                EventsReporterWrapper.ReportEvent(LogsViewerLogsLoadedEvent.Create(CommandStatus.Failure));
                throw;
            }
        }
        /// <summary>
        /// Repeatedly make list log entries request till it gets desired number of logs or it reaches end.
        /// _nextPageToken is used to control if it is getting first page or continuous page.
        ///
        /// On complex filters, scanning through logs take time. The server returns empty results
        ///   with a next page token. Continue to send request till some logs are found.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token. The caller can monitor the cancellation event.</param>
        /// <param name="autoReload">Indicate if the request comes from autoReload event.</param>
        private async Task LoadLogsAsync(CancellationToken cancellationToken, bool autoReload = false)
        {
            if (_logs.Count >= MaxLogEntriesCount)
            {
                UserPromptUtils.ErrorPrompt(
                    message: Resources.LogViewerResultSetTooLargeMessage,
                    title: Resources.UiDefaultPromptTitle);
                _cancellationTokenSource?.Cancel();
                return;
            }

            try
            {
                var startTimestamp = DateTime.Now;

                var order = DateTimePickerModel.IsDescendingOrder ? "timestamp desc" : "timestamp asc";
                int count = 0;
                while (count < DefaultPageSize && !cancellationToken.IsCancellationRequested)
                {
                    Debug.WriteLine($"LoadLogs, count={count}, firstPage={_nextPageToken == null}");

                    // Here, it does not do pageSize: _defaultPageSize - count,
                    // Because this is requried to use same page size for getting next page.
                    var results = await _dataSource.Value.ListLogEntriesAsync(_filter, order,
                                                                              pageSize : DefaultPageSize, nextPageToken : _nextPageToken, cancelToken : cancellationToken);

                    _nextPageToken = results.NextPageToken;
                    if (results?.LogEntries != null)
                    {
                        count += results.LogEntries.Count;
                        AddLogs(results.LogEntries, autoReload);
                    }

                    if (String.IsNullOrWhiteSpace(_nextPageToken))
                    {
                        _nextPageToken = null;
                        break;
                    }
                }

                EventsReporterWrapper.ReportEvent(LogsViewerLogsLoadedEvent.Create(CommandStatus.Success, DateTime.Now - startTimestamp));
            }
            catch (Exception ex) when(!ErrorHandlerUtils.IsCriticalException(ex))
            {
                EventsReporterWrapper.ReportEvent(LogsViewerLogsLoadedEvent.Create(CommandStatus.Failure));
                throw;
            }
        }