Ejemplo n.º 1
0
 public static void RequestLog(this ILogger logger, HttpRequestLog requestLog) => logger.Log(
     LogLevel.Information,
     new EventId(1, "RequestLogLog"),
     requestLog,
     exception: null,
     formatter: HttpRequestLog.Callback);
Ejemplo n.º 2
0
        private async Task InvokeInternal(HttpContext context)
        {
            var options = _options.CurrentValue;
            RequestBufferingStream?requestBufferingStream = null;
            Stream?originalBody = null;

            if ((HttpLoggingFields.Request & options.LoggingFields) != HttpLoggingFields.None)
            {
                var request = context.Request;
                var list    = new List <KeyValuePair <string, object?> >(
                    request.Headers.Count + DefaultRequestFieldsMinusHeaders);

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestProtocol))
                {
                    AddToList(list, nameof(request.Protocol), request.Protocol);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestMethod))
                {
                    AddToList(list, nameof(request.Method), request.Method);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestScheme))
                {
                    AddToList(list, nameof(request.Scheme), request.Scheme);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestPath))
                {
                    AddToList(list, nameof(request.PathBase), request.PathBase);
                    AddToList(list, nameof(request.Path), request.Path);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestQuery))
                {
                    AddToList(list, nameof(request.QueryString), request.QueryString.Value);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestHeaders))
                {
                    FilterHeaders(list, request.Headers, options._internalRequestHeaders);
                }

                if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestBody))
                {
                    if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType,
                                                                    options.MediaTypeOptions.MediaTypeStates,
                                                                    out var encoding))
                    {
                        originalBody           = request.Body;
                        requestBufferingStream = new RequestBufferingStream(
                            request.Body,
                            options.RequestBodyLogLimit,
                            _logger,
                            encoding);
                        request.Body = requestBufferingStream;
                    }
                    else
                    {
                        _logger.UnrecognizedMediaType();
                    }
                }

                var httpRequestLog = new HttpRequestLog(list);

                _logger.RequestLog(httpRequestLog);
            }

            ResponseBufferingStream? responseBufferingStream = null;
            IHttpResponseBodyFeature?originalBodyFeature     = null;

            try
            {
                var response = context.Response;

                if (options.LoggingFields.HasFlag(HttpLoggingFields.ResponseBody))
                {
                    originalBodyFeature = context.Features.Get <IHttpResponseBodyFeature>() !;

                    // TODO pool these.
                    responseBufferingStream = new ResponseBufferingStream(originalBodyFeature,
                                                                          options.ResponseBodyLogLimit,
                                                                          _logger,
                                                                          context,
                                                                          options.MediaTypeOptions.MediaTypeStates,
                                                                          options);
                    response.Body = responseBufferingStream;
                    context.Features.Set <IHttpResponseBodyFeature>(responseBufferingStream);
                }

                await _next(context);

                if (requestBufferingStream?.HasLogged == false)
                {
                    // If the middleware pipeline didn't read until 0 was returned from readasync,
                    // make sure we log the request body.
                    requestBufferingStream.LogRequestBody();
                }

                if (responseBufferingStream == null || responseBufferingStream.FirstWrite == false)
                {
                    // No body, write headers here.
                    LogResponseHeaders(response, options, _logger);
                }

                if (responseBufferingStream != null)
                {
                    var responseBody = responseBufferingStream.GetString(responseBufferingStream.Encoding);
                    if (!string.IsNullOrEmpty(responseBody))
                    {
                        _logger.ResponseBody(responseBody);
                    }
                }
            }
            finally
            {
                responseBufferingStream?.Dispose();

                if (originalBodyFeature != null)
                {
                    context.Features.Set(originalBodyFeature);
                }

                requestBufferingStream?.Dispose();

                if (originalBody != null)
                {
                    context.Request.Body = originalBody;
                }
            }
        }