Пример #1
0
        /// <summary>
        /// Use this extension method to ensure Outgoing Service Requests get logged
        /// with the correct Dependency Information.
        /// </summary>
        public static Task <HttpResponseMessage> SendAsync(
            this HttpClient httpClient,
            HttpRequestMessage requestMessage,
            string operationName,
            string dependencyOperationName,
            string dependencyOperationVersion)
        {
            string correlationVector = CorrelationVector.Current?.Increment();

            if (!string.IsNullOrWhiteSpace(correlationVector))
            {
                requestMessage.Headers.Add(CorrelationVector.HeaderName, correlationVector);

                var dependencyOperationInfo = new DependencyOperationInfo()
                {
                    OperationName              = operationName,
                    DependencyOperationName    = dependencyOperationName,
                    DependencyOperationVersion = dependencyOperationVersion
                };

                // Add the dependency information to the cache so it can be used for logging
                //
                DependencyInfoCache.Add(correlationVector, dependencyOperationInfo);

                // Add the dependency information to the request message properties so the
                // client handler can add the dependency name and type
                //
                requestMessage.Properties.Add(
                    DependencyClientHandler.RequestPropertyKey,
                    dependencyOperationInfo);
            }

            return(httpClient.SendAsync(requestMessage));
        }
            private void LogHttpOutComplete(HttpWebRequest requestMessage, HttpStatusCode responseStatus, string responseSize)
            {
                long   latency           = (long)(DateTime.Now - requestMessage.Date).TotalMilliseconds;
                string correlationVector = requestMessage.Headers["MS-CV"];
                DependencyOperationInfo dependencyOperationInfo = null;

                if (!string.IsNullOrWhiteSpace(correlationVector) &&
                    HttpClientExtensions.DependencyInfoCache.ContainsKey(correlationVector))
                {
                    dependencyOperationInfo = HttpClientExtensions.DependencyInfoCache[correlationVector];
                }

                // These properties can be gleaned from the Http request and response
                //
                Console.WriteLine("---------------------------------------");
                Console.WriteLine("Logging the Outgoing Service Request...");
                Console.WriteLine("Correlation Vector: {0}", requestMessage.Headers["MS-CV"]);
                Console.WriteLine("Target Uri: {0}", requestMessage.RequestUri.ToString());
                Console.WriteLine("Latency ms: {0}", latency);
                // TODO - need code to be able to read the service error code - should this code be common or customizable?
                Console.WriteLine("Service error code: {0}", null);
                Console.WriteLine("Succeeded: {0}", (int)responseStatus < 400);
                Console.WriteLine("Request method: {0}", requestMessage.Method);
                Console.WriteLine("Protocol Status Code: {0}", (int)responseStatus);

                Console.WriteLine("Response Size (bytes): {0}", responseSize);

                // These properties need to be stamped on the request by the service owner
                // through the use of DependencyClientHandler and a HttpClient.SendAsync
                // extension method.
                //
                if (dependencyOperationInfo != null)
                {
                    Console.WriteLine("Dependency Name: {0}", dependencyOperationInfo.DependencyName);
                    Console.WriteLine("Dependency Type: {0}", dependencyOperationInfo.DependencyType);
                    Console.WriteLine("Operation Name: {0}", dependencyOperationInfo.OperationName);
                    Console.WriteLine("Dependency Operation Name: {0}", dependencyOperationInfo.DependencyOperationName);
                    Console.WriteLine("Dependency Operation Version: {0}", dependencyOperationInfo.DependencyOperationVersion);
                }

                Console.WriteLine("---------------------------------------");

                if (dependencyOperationInfo != null)
                {
                    HttpClientExtensions.DependencyInfoCache.Remove(correlationVector);
                }
            }