예제 #1
0
        public async Task Invoke(HttpContext httpContext)
        {
            IWorkContext workContext = httpContext.Items.Get <IWorkContext>() ?? WorkContext.Empty;
            var          builder     = new WorkContextBuilder(workContext);

            builder.SetContainer(_serviceConfiguration.Container);

            foreach (var item in httpContext.Request.Headers)
            {
                var property = _headerFactory.Create(item.Key, item.Value);
                if (property != null)
                {
                    builder.Properties.Set(property, derivedType: property.GetType());
                }
            }

            // Correlation Vector
            var headerCv = builder.Properties.Get <CvHeader>();

            if (headerCv != null)
            {
                builder.Cv = new CorrelationVector(headerCv.Value);
            }

            workContext = builder.Build();

            Uri url            = new Uri(httpContext.Request.GetEncodedUrl());
            var requestContext = new RequestContext(workContext, httpContext.Request.Method, url);

            httpContext.Items.Set(requestContext);

            using (var scope = new TimerScope(
                       () => _webLogEvent.HttpRequestStart(workContext, requestContext),
                       (x) => _webLogEvent.HttpRequestStop(workContext, requestContext, x)))
            {
                await _next.Invoke(httpContext);

                if (httpContext?.Response?.StatusCode != null)
                {
                    AspMvcEventSource.Log.Verbose(workContext, $"REST response result: {httpContext.Response.StatusCode}");
                }
            }
        }