private static List <KeyValuePair <string, object> > GetLogScope(
            CorrelationContext correlationContext)
        {
            Ensure.Any.IsNotNull(correlationContext, nameof(correlationContext));

            return(new List <KeyValuePair <string, object> >
            {
                GetCorrelationContextLogProperty(
                    nameof(CorrelationContext.Id),
                    correlationContext.Id)
            });
        }
        public async Task Invoke(HttpContext context, IServiceProvider serviceProvider)
        {
            var correlationContext = new CorrelationContext
            {
                Id = TryParseGuid(GetHeaderOrDefault(context, Constants.CorrelationIdHeader)) ?? Guid.NewGuid()
            };

            context.Response?.Headers?.Add(Constants.CorrelationIdHeader, correlationContext.Id.ToString());

            using (_logger.BeginScope(GetLogScope(correlationContext)))
            {
                await _next(context);
            }
        }