Пример #1
0
 internal FailedToFetchConfigException(string message, WaitInfoS waitInfo, LogLevel severity = LogLevel.Error, Exception cause = null)
     : base(message, cause)
 {
     Severity = severity;
     WaitInfo = waitInfo;
 }
        protected override async Task WorkLoopIteration()
        {
            ++_dbgIterationsCount;
            var                 waitingLogSeverity = LogLevel.Trace;
            WaitInfoS           waitInfo;
            HttpRequestMessage  httpRequest      = null;
            HttpResponseMessage httpResponse     = null;
            string              httpResponseBody = null;

            try
            {
                httpRequest = BuildHttpRequest(_eTag);

                (httpResponse, httpResponseBody) = await FetchConfigHttpResponseAsync(httpRequest);

                CentralConfigReader centralConfigReader;
                (centralConfigReader, waitInfo) = _centralConfigResponseParser.ParseHttpResponse(httpResponse, httpResponseBody);
                if (centralConfigReader != null)
                {
                    UpdateConfigStore(centralConfigReader);
                    _eTag = httpResponse.Headers.ETag;
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception ex)
            {
                var severity = LogLevel.Error;

                if (ex is FailedToFetchConfigException fEx)
                {
                    severity = fEx.Severity;
                    waitInfo = fEx.WaitInfo;
                }
                else
                {
                    waitInfo = new WaitInfoS(WaitTimeIfAnyError, "Default wait time is used because exception was thrown"
                                             + " while fetching configuration from APM Server and parsing it.");
                }

                if (severity == LogLevel.Error)
                {
                    waitingLogSeverity = LogLevel.Information;
                }

                _logger.IfLevel(severity)
                ?.LogException(ex, "Exception was thrown while fetching configuration from APM Server and parsing it."
                               + " ETag: `{ETag}'. URL: `{Url}'. Apm Server base URL: `{ApmServerUrl}'. WaitInterval: {WaitInterval}."
                               + " dbgIterationsCount: {dbgIterationsCount}."
                               + Environment.NewLine + "+-> Request:{HttpRequest}"
                               + Environment.NewLine + "+-> Response:{HttpResponse}"
                               + Environment.NewLine + "+-> Response body [length: {HttpResponseBodyLength}]:{HttpResponseBody}"
                               , _eTag.AsNullableToString(), _getConfigAbsoluteUrl, HttpClientInstance.BaseAddress, waitInfo.Interval.ToHms(),
                               _dbgIterationsCount
                               , httpRequest == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpRequest.ToString())
                               , httpResponse == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpResponse.ToString())
                               , httpResponseBody == null ? "N/A" : httpResponseBody.Length.ToString()
                               , httpResponseBody == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpResponseBody));
            }
            finally
            {
                httpRequest?.Dispose();
                httpResponse?.Dispose();
            }

            _logger.IfLevel(waitingLogSeverity)
            ?.Log("Waiting {WaitInterval}... {WaitReason}. dbgIterationsCount: {dbgIterationsCount}."
                  , waitInfo.Interval.ToHms(), waitInfo.Reason, _dbgIterationsCount);
            await _agentTimer.Delay(_agentTimer.Now + waitInfo.Interval, CtsInstance.Token);
        }