/// <summary> /// Evaluate connection string and return the requested endpoint. /// </summary> /// <remarks> /// Parsing the connection string MUST follow these rules: /// 1. check for explicit endpoint (location is ignored) /// 2. check for endpoint suffix (location is optional) /// 3. use default endpoint (location is ignored) /// This behavior is required by the Connection String Specification. /// </remarks> internal static string GetIngestionEndpoint(this Azure.Core.ConnectionString connectionString) { // Passing the user input values through the Uri constructor will verify that we've built a valid endpoint. Uri uri; if (connectionString.TryGetNonRequiredValue(Constants.IngestionExplicitEndpointKey, out string explicitEndpoint)) { if (!Uri.TryCreate(explicitEndpoint, UriKind.Absolute, out uri)) { throw new ArgumentException($"The value for {Constants.IngestionExplicitEndpointKey} is invalid. '{explicitEndpoint}'"); } } else if (connectionString.TryGetNonRequiredValue(Constants.EndpointSuffixKey, out string endpointSuffix)) { var location = connectionString.GetNonRequired(Constants.LocationKey); if (!TryBuildUri(prefix: Constants.IngestionPrefix, suffix: endpointSuffix, location: location, uri: out uri)) { throw new ArgumentException($"The value for {Constants.EndpointSuffixKey} is invalid. '{endpointSuffix}'"); } } else { return(Constants.DefaultIngestionEndpoint); } return(uri.AbsoluteUri); }
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")); }
/// <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)); }
internal static string GetInstrumentationKey(this Azure.Core.ConnectionString connectionString) => connectionString.GetRequired(Constants.InstrumentationKeyKey);
/// <summary> /// This method wraps <see cref="Azure.Core.ConnectionString.GetNonRequired(string)"/> in a null check. /// </summary> internal static bool TryGetNonRequiredValue(this Azure.Core.ConnectionString connectionString, string key, out string value) { value = connectionString.GetNonRequired(key); return(value != null); }