public void TestParse_WithExtraSpaces()
        {
            var test = AzureCoreConnectionString.Parse(" key1 =  value1   ; key2 = value2 ; key3    =value3   ");

            Assert.Equal("value1", test.GetRequired("key1"));
            Assert.Equal("value2", test.GetRequired("key2"));
            Assert.Equal("value3", test.GetRequired("key3"));
        }
        public void TestParse_WithTrailingSemicolon()
        {
            var test = AzureCoreConnectionString.Parse("key1=value1;key2=value2;key3=value3;");

            Assert.Equal("value1", test.GetRequired("key1"));
            Assert.Equal("value2", test.GetRequired("key2"));
            Assert.Equal("value3", test.GetRequired("key3"));
        }
        public void TestParse_IsCaseInsensitive()
        {
            var test = AzureCoreConnectionString.Parse("UPPERCASE=value1;lowercase=value2;MixedCase=value3");

            Assert.Equal("value1", test.GetRequired("UPPERCASE"));
            Assert.Equal("value1", test.GetRequired("uppercase"));
            Assert.Equal("value2", test.GetRequired("LOWERCASE"));
            Assert.Equal("value2", test.GetRequired("lowercase"));
            Assert.Equal("value3", test.GetRequired("MIXEDCASE"));
            Assert.Equal("value3", test.GetRequired("mixedcase"));
        }
Exemple #4
0
        /// <summary>
        /// Parse a connection string that matches the format: "key1=value1;key2=value2;key3=value3".
        /// This method will encapsulate all exception handling.
        /// </summary>
        /// <remarks>
        /// Official Doc: <a href="https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string" />.
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Any exceptions that occur while parsing the connection string will be wrapped and re-thrown.
        /// </exception>
        public static void GetValues(string connectionString, out string instrumentationKey, out string ingestionEndpoint)
        {
            try
            {
                if (connectionString == null)
                {
                    throw new ArgumentNullException(nameof(connectionString));
                }
                else if (connectionString.Length > Constants.ConnectionStringMaxLength)
                {
                    throw new ArgumentOutOfRangeException(nameof(connectionString), $"Values greater than {Constants.ConnectionStringMaxLength} characters are not allowed.");
                }

                var connString = AzureCoreConnectionString.Parse(connectionString);
                instrumentationKey = connString.GetInstrumentationKey();
                ingestionEndpoint  = connString.GetIngestionEndpoint();
            }
            catch (Exception ex)
            {
                AzureMonitorExporterEventSource.Log.Write($"ConnectionStringError{EventLevelSuffix.Error}", ex);
                throw new InvalidOperationException("Connection String Error: " + ex.Message, ex);
            }
        }
 public void TestParse_WithInvalidDelimiters()
 {
     Assert.Throws <InvalidOperationException>(() => AzureCoreConnectionString.Parse("key1;key2=value2"));
 }
 public void TestParse_WithDuplaceKeysWithSpaces()
 {
     Assert.Throws <InvalidOperationException>(() => AzureCoreConnectionString.Parse("key1=value1;key1  =value2"));
 }
 public void TestParse_WithEmptyString()
 {
     Assert.Throws <InvalidOperationException>(() => AzureCoreConnectionString.Parse(string.Empty));
 }
 public void TestParse_WithNull()
 {
     Assert.Throws <NullReferenceException>(() => AzureCoreConnectionString.Parse(null));
 }