コード例 #1
0
 public RequestIdMiddleware(RequestDelegate next,
                            IRequestId requestID,
                            ILogger <RequestIdMiddleware> logger)
 {
     _next   = next;
     _logger = logger;
 }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: RequestId.cs プロジェクト: cbeall/RequestChain
        public bool Equals(IRequestId other)
        {
            if (other == null)
            {
                return(false);
            }

            return(Value.Equals(other.Value) &&
                   Equals(Depth, other.Depth));
        }
コード例 #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);
        }
コード例 #5
0
        public Task Invoke(HttpContext context, IRequestId requestId)
        {
            _logger.LogInformation($"Request {requestId.Id} executing");

            return(_next(context));
        }
コード例 #6
0
        public Task Invoke(HttpContext context, IRequestId requestId)
        {
            _logger.LogInformation($"Request {requestId.Id} executing");

            return _next(context);
        }
コード例 #7
0
 public static void ApplyRequestChainHeader(this HttpRequestMessage request, IRequestId requestId)
 {
     request.Headers.ApplyRequestChainHeader(requestId);
 }
コード例 #8
0
 public GenericResponse(IRequestId requestId, IStatus status, ITypedObject data = null)
 {
     RequestId = requestId;
     Status    = status;
     Data      = data;
 }
コード例 #9
0
 public bool Equals(IRequestId other)
 {
     throw new NotImplementedException();
 }
コード例 #10
0
 public HomeController(IRequestId requestId)
 {
     _requestId = requestId;
 }
コード例 #11
0
 /// <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);
 }
コード例 #12
0
 /// <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));
 }
コード例 #13
0
 /// <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));
 }