Beispiel #1
0
 public RequestIdMiddleware(RequestDelegate next,
                            IRequestId requestID,
                            ILogger <RequestIdMiddleware> logger)
 {
     _next   = next;
     _logger = logger;
 }
        public static void ApplyRequestChainHeader(this HttpRequestHeaders headers, IRequestId requestId)
        {
            string headerValue = requestId.Value.ToString();

            if (requestId.Depth.HasValue)
            {
                var newDepth = requestId.Depth.Value + 1;
                headerValue = $"{requestId.Value}:{newDepth}";
            }

            var existingHeader = headers
                                 .FirstOrDefault(a => string.Equals(a.Key, requestId.RequestChainHeaderKey));

            if (!Equals(existingHeader, default(KeyValuePair <string, IEnumerable <string> >)))
            {
                if (existingHeader.Value.Any(a => string.Equals(a, headerValue, StringComparison.OrdinalIgnoreCase)))
                {
                    // Header is already in place... exit no need to proceed
                    return;
                }
                else
                {
                    var firstHeader = existingHeader.Value.FirstOrDefault();
                    var msg         = $"Attempted to set RequestChainHeader when it already exists and does not match (\"{firstHeader}\")";
                    throw new InvalidOperationException(msg);
                }
            }

            headers.Add(requestId.RequestChainHeaderKey, headerValue);
        }
Beispiel #3
0
        public bool Equals(IRequestId other)
        {
            if (other == null)
            {
                return(false);
            }

            return(Value.Equals(other.Value) &&
                   Equals(Depth, other.Depth));
        }
Beispiel #4
0
        public async Task Invoke(HttpContext context, IRequestId requestId, RequestChainOptions options)
        {
            DateTime start = DateTime.Now;

            // Gets the request scoped IRequestId as the underlying class to have access to internal set methods
            RequestId settableRequestId = requestId as RequestId;

            // Set the request id based on the current request context
            settableRequestId.SetRequestId(context.Request, options);

            // Get string formatted depth for logging
            string requestDepth = string.Empty;

            if (options.IncludeRequestDepth && settableRequestId.HasValidDepth)
            {
                requestDepth = $"(Depth {requestId.Depth}) ";
            }

            // Log start of request
            _logger.Log(options.RequestBeginEndLogLevel,
                        "Begin request {0} {1}for {2} on {3}",
                        requestId.Value, requestDepth, context.Request.Path, context.Request.PathBase);

            // Execute actual request logic
            await _next.Invoke(context);

            // Get status code for logging
            string statusCodeStr = string.Empty;

            if (context.Response.StatusCode != default(int))
            {
                statusCodeStr = $" with status code {context.Response.StatusCode} ";
            }

            // Log end of request
            TimeSpan requestTime = DateTime.Now - start;

            _logger.Log(options.RequestBeginEndLogLevel, "End request {0} {2} ms{1}",
                        requestId.Value, requestTime.Milliseconds, statusCodeStr);
        }
        public Task Invoke(HttpContext context, IRequestId requestId)
        {
            _logger.LogInformation($"Request {requestId.Id} executing");

            return(_next(context));
        }
        public Task Invoke(HttpContext context, IRequestId requestId)
        {
            _logger.LogInformation($"Request {requestId.Id} executing");

            return _next(context);
        }
 public static void ApplyRequestChainHeader(this HttpRequestMessage request, IRequestId requestId)
 {
     request.Headers.ApplyRequestChainHeader(requestId);
 }
Beispiel #8
0
 public GenericResponse(IRequestId requestId, IStatus status, ITypedObject data = null)
 {
     RequestId = requestId;
     Status    = status;
     Data      = data;
 }
 public bool Equals(IRequestId other)
 {
     throw new NotImplementedException();
 }
Beispiel #10
0
 public HomeController(IRequestId requestId)
 {
     _requestId = requestId;
 }
 /// <summary>
 /// Applies request chain header to DefaultRequestHeaders of HttpClient if it is not already attached.
 /// The depth will be incremented by one from the current depth.
 /// </summary>
 /// <param name="httpClient">Client to make outbound calls</param>
 /// <param name="requestId">The current request Id for the service</param>
 /// <returns><see cref="HttpClient"/> with RequestChain header attached</returns>
 public static HttpClient ApplyRequestChain(this HttpClient httpClient, IRequestId requestId)
 {
     httpClient.DefaultRequestHeaders.ApplyRequestChainHeader(requestId);
     return(httpClient);
 }
 /// <summary>
 /// Initalizes a new instance of <see cref="HttpClient"/> with a specific handler.
 /// </summary>
 /// <param name="requestId">The current request Id for the service</param>
 /// <param name="handler">The <see cref="HttpMessageHandler"/> responsible for processing the HTTP response
 /// message.
 /// <param name="disposeHandler">true if the inner handler should be disposed of by Dispose(), false if
 /// you intend to reuse the inner handler.</param>
 /// <returns><see cref="HttpClient"/> with RequestChain header attached</returns>
 public static HttpClient CreateHttpClient(this IRequestId requestId, HttpMessageHandler handler, bool disposeHandler)
 {
     return(new HttpClient(handler, disposeHandler)
            .ApplyRequestChain(requestId));
 }
 /// <summary>
 /// Initalizes a new instance of <see cref="HttpClient"/>
 /// </summary>
 /// <param name="requestId">The current request Id for the service</param>
 /// <returns><see cref="HttpClient"/> with RequestChain header attached</returns>
 public static HttpClient CreateHttpClient(this IRequestId requestId)
 {
     return(new HttpClient()
            .ApplyRequestChain(requestId));
 }