Exemplo n.º 1
0
        /// <inheritdoc cref="IContextLogger" />
        public void LogError(Exception exception, IStopwatch?stopwatch)
        {
            _statusCode = 500;
            var request  = RequestDetails.From(_context.Request);
            var response = ResponseDetails.From(_context.Response, stopwatch, _statusCode);

            _httpLogger.LogError(exception, request, response);
        }
        private StringBuilder ResponseHead(RequestDetails request, ResponseDetails response)
        {
            var status = $"{(int?)response.StatusCode} {response.StatusCode}";
            var text   = new StringBuilder($"{request.Protocol} {status}{NewLine}");

            AppendHeaders(text, response.Headers);

            return(text);
        }
        /// <inheritdoc />
        public async Task LogResponse(RequestDetails request, ResponseDetails response)
        {
            if (ShouldSkip(request))
            {
                return;
            }

            using var requestScope  = RequestScope(request);
            using var responseScope = ResponseScope(response);
            await _responseLogger.LogResponse(_logger, request, response);
        }
        /// <inheritdoc cref="IBasicInfoLogger"/>
        public void LogBasicInfo(ILogger logger, RequestDetails request, ResponseDetails response)
        {
            if (logger.GetLogLevel() > LogLevel.Information)
            {
                return;
            }

            var status  = $"{(int?)response.StatusCode} {response.StatusCode}";
            var message = $"{request.Method} {request.Url} at {response.Time} with {status}";

            logger.LogInformation(message);
        }
        /// <inheritdoc />
        public Task LogInfo(RequestDetails request, ResponseDetails response)
        {
            if (ShouldSkip(request))
            {
                return(Task.CompletedTask);
            }

            using var requestScope  = RequestScope(request);
            using var responseScope = ResponseScope(response);

            _basicInfoLogger.LogBasicInfo(_logger, request, response);

            return(Task.CompletedTask);
        }
        /// <inheritdoc cref="IResponseLogger"/>
        public async Task LogResponse(ILogger logger, RequestDetails request, ResponseDetails response)
        {
            var level = logger.GetLogLevel();

            if (level > LogLevel.Debug)
            {
                return;
            }

            StringBuilder text = ResponseHead(request, response);

            if (level <= LogLevel.Trace)
            {
                text.AppendLine(await ReadBody(response));
            }

            logger.Log(level, text.ToString());
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
        {
            var httpLogger = _httpLoggerFactory.Create(_logger);
            var measure    = _measure.StartMeasure();
            HttpResponseMessage?response        = null;
            RequestDetails      requestDetails  = RequestDetails.From(request);
            ResponseDetails?    responseDetails = null;

            try
            {
                await httpLogger.LogRequest(requestDetails);

                response = await base.SendAsync(request, ct);

                var stopwatch = measure.StopMeasure();

                responseDetails = ResponseDetails.From(response, stopwatch);
                await httpLogger.LogResponse(requestDetails, responseDetails);

                return(response);
            }
            catch (Exception exception)
            {
                var stopwatch = measure.StopMeasure();
                httpLogger.LogError(
                    exception,
                    requestDetails,
                    responseDetails ?? ResponseDetails.From(response, stopwatch));

                throw;
            }
            finally
            {
                var stopwatch = measure.StopMeasure();
                httpLogger.LogInfo(
                    requestDetails,
                    responseDetails ?? ResponseDetails.From(response, stopwatch)).Wait(ct);
            }
        }
        private IDisposable ResponseScope(ResponseDetails response)
        {
            ResponseScope scope = new((int?)response.StatusCode, response.Stopwatch);

            return(_logger.BeginScope(scope));
        }
 private Task <string> ReadBody(ResponseDetails response)
 {
     return(ReadContent(response.ContentType, response.Content));
 }