/// <summary>
        /// For the given header collection, for a given header of name-value type, return list of KeyValuePairs.
        /// </summary>
        /// <param name="headers">Header collection.</param>
        /// <param name="headerName">Name of the header in the collection.</param>
        /// <returns>List of KeyValuePairs in the given header.</returns>
        public static IDictionary <string, string> GetNameValueCollectionFromHeader(this NameValueCollection headers, string headerName)
        {
            Debug.Assert(headerName != null, "headerName must not be null");

            IEnumerable <string> headerValue = headers.GetHeaderValue(headerName);

            return(HeadersUtilities.GetHeaderDictionary(headerValue));
        }
        /// <summary>
        /// For the given header collection, adds KeyValuePair to header.
        /// </summary>
        /// <param name="headers">Header collection.</param>
        /// <param name="headerName">Name of the header that is to contain the name-value pair.</param>
        /// <param name="keyName">Name in the name value pair.</param>
        /// <param name="value">Value in the name value pair.</param>
        public static void SetNameValueHeaderValue(this NameValueCollection headers, string headerName, string keyName, string value)
        {
            Debug.Assert(headerName != null, "headerName must not be null");
            Debug.Assert(keyName != null, "keyName must not be null");

            IEnumerable <string> headerValue = headers.GetHeaderValue(headerName);

            headers[headerName] = string.Join(",", HeadersUtilities.UpdateHeaderWithKeyValue(headerValue, keyName, value));
        }
        /// <summary>
        /// For the given header collection, for a given header of name-value type, find the value of a particular key.
        /// </summary>
        /// <param name="headers">Header collection.</param>
        /// <param name="headerName">Name of the header in the collection.</param>
        /// <param name="keyName">Desired key of the key-value list.</param>
        /// <returns>Value against the given parameters.</returns>
        public static string GetNameValueHeaderValue(this NameValueCollection headers, string headerName, string keyName)
        {
            Debug.Assert(headerName != null, "headerName must not be null");
            Debug.Assert(keyName != null, "keyName must not be null");

            IEnumerable <string> headerValue = headers.GetHeaderValue(headerName);

            return(HeadersUtilities.GetHeaderKeyValue(headerValue, keyName));
        }
        /// <summary>
        /// Format and store an iKey and appId pair into the dictionary of known correlation ids.
        /// </summary>
        /// <param name="ikey">Instrumentation Key is expected to be a Guid string.</param>
        /// <param name="appId">Application Id is expected to be a Guid string. App Id needs to be Http Header safe, and all non-ASCII characters will be removed.</param>
        private void GenerateCorrelationIdAndAddToDictionary(string ikey, string appId)
        {
            if (string.IsNullOrWhiteSpace(appId))
            {
                return;
            }

            appId = HeadersUtilities.SanitizeString(appId);
            if (!string.IsNullOrEmpty(appId))
            {
                this.knownCorrelationIds[ikey] = string.Format(CultureInfo.InvariantCulture, CorrelationIdFormat, appId);
            }
        }
        /// <summary>
        /// Format and store an iKey and appId pair into the dictionary of known correlation ids.
        /// </summary>
        /// <param name="ikey">Instrumentation Key is expected to be a Guid string.</param>
        /// <param name="appId">Application Id is expected to be a Guid string. App Id needs to be Http Header safe, and all non-ASCII characters will be removed.</param>
        /// <remarks>To protect against injection attacks, AppId will be truncated to a maximum length.</remarks>
        private void GenerateCorrelationIdAndAddToDictionary(string ikey, string appId)
        {
            // Arbitrary maximum length to guard against injections.
            appId = StringUtilities.EnforceMaxLength(appId, InjectionGuardConstants.AppIdMaxLengeth);

            if (string.IsNullOrWhiteSpace(appId))
            {
                return;
            }

            appId = HeadersUtilities.SanitizeString(appId);
            if (!string.IsNullOrEmpty(appId))
            {
                this.knownCorrelationIds[ikey] = string.Format(CultureInfo.InvariantCulture, CorrelationIdFormat, appId);
            }
        }