/// <summary>
        /// Creates a <see cref="IConfigurationSource"/> by combining environment variables,
        /// AppSettings where available, and a local datadog.json file, if present.
        /// </summary>
        /// <returns>A new <see cref="IConfigurationSource"/> instance.</returns>
        public static CompositeConfigurationSource CreateDefaultConfigurationSource()
        {
            // env > AppSettings > datadog.json
            var configurationSource = new CompositeConfigurationSource
            {
                new EnvironmentConfigurationSource(),

#if !NETSTANDARD2_0
                // on .NET Framework only, also read from app.config/web.config
                new NameValueConfigurationSource(System.Configuration.ConfigurationManager.AppSettings)
#endif
            };

            // if environment variable is not set, look for default file name in the current directory
            var configurationFileName = configurationSource.GetString(ConfigurationKeys.ConfigurationFileName) ??
                                        Path.Combine(System.Environment.CurrentDirectory, "datadog.json");

            if (Path.GetExtension(configurationFileName).ToUpperInvariant() == ".JSON" &&
                File.Exists(configurationFileName))
            {
                configurationSource.Add(JsonConfigurationSource.FromFile(configurationFileName));
            }

            return(configurationSource);
        }
Exemple #2
0
        private static bool TryLoadJsonConfigurationFile(IConfigurationSource configurationSource, out IConfigurationSource jsonConfigurationSource)
        {
            try
            {
                // if environment variable is not set, look for default file name in the current directory
                var configurationFileName = configurationSource.GetString(ConfigurationKeys.ConfigurationFileName) ??
                                            configurationSource.GetString("DD_DOTNET_TRACER_CONFIG_FILE") ??
                                            Path.Combine(GetCurrentDirectory(), "datadog.json");

                if (string.Equals(Path.GetExtension(configurationFileName), ".JSON", StringComparison.OrdinalIgnoreCase) &&
                    File.Exists(configurationFileName))
                {
                    jsonConfigurationSource = JsonConfigurationSource.FromFile(configurationFileName);
                    return(true);
                }
            }
            catch (Exception)
            {
                // Unable to load the JSON file from disk
                // The configuration manager should not depend on a logger being bootstrapped yet
                // so do not do anything
            }

            jsonConfigurationSource = default;
            return(false);
        }
        private static bool TryLoadJsonConfigurationFile(string configurationFileName, out JsonConfigurationSource jsonConfigurationSource)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(configurationFileName) &&
                    string.Equals(Path.GetExtension(configurationFileName), ".JSON", StringComparison.OrdinalIgnoreCase) &&
                    File.Exists(configurationFileName))
                {
                    jsonConfigurationSource = JsonConfigurationSource.FromFile(configurationFileName);
                    return(true);
                }
            }
            catch (Exception)
            {
                // Unable to load the JSON file from disk
                // The configuration manager should not depend on a logger being bootstrapped yet
                // so do not do anything
            }

            jsonConfigurationSource = default;
            return(false);
        }
        /// <summary>
        /// Creates a <see cref="IConfigurationSource"/> by combining environment variables,
        /// AppSettings where available, and a local datadog.json file, if present.
        /// </summary>
        /// <returns>A new <see cref="IConfigurationSource"/> instance.</returns>
        internal static CompositeConfigurationSource CreateDefaultConfigurationSource()
        {
            // env > AppSettings > datadog.json
            var configurationSource = new CompositeConfigurationSource
            {
                new EnvironmentConfigurationSource(),

#if NETFRAMEWORK
                // on .NET Framework only, also read from app.config/web.config
                new NameValueConfigurationSource(System.Configuration.ConfigurationManager.AppSettings)
#endif
            };

            string currentDirectory = System.Environment.CurrentDirectory;

#if NETFRAMEWORK
            // on .NET Framework only, use application's root folder
            // as default path when looking for datadog.json
            if (System.Web.Hosting.HostingEnvironment.IsHosted)
            {
                currentDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~");
            }
#endif

            // if environment variable is not set, look for default file name in the current directory
            var configurationFileName = configurationSource.GetString(ConfigurationKeys.ConfigurationFileName) ??
                                        configurationSource.GetString("DD_DOTNET_TRACER_CONFIG_FILE") ??
                                        Path.Combine(currentDirectory, "datadog.json");

            if (Path.GetExtension(configurationFileName).ToUpperInvariant() == ".JSON" &&
                File.Exists(configurationFileName))
            {
                configurationSource.Add(JsonConfigurationSource.FromFile(configurationFileName));
            }

            return(configurationSource);
        }
        private static bool TryLoadJsonConfigurationFile(IConfigurationSource configurationSource, out JsonConfigurationSource jsonConfigurationSource)
        {
            // if environment variable is not set, look for default file name in the current directory
            var configurationFileName = configurationSource.GetString(ConfigurationKeys.ConfigurationFileName) ??
                                        configurationSource.GetString("OTEL_DOTNET_TRACER_CONFIG_FILE");

            return(TryLoadJsonConfigurationFile(configurationFileName, out jsonConfigurationSource));
        }
        private static bool TryLoadPluginJsonConfigurationFile(IConfigurationSource configurationSource, out JsonConfigurationSource jsonConfigurationSource)
        {
            var configurationFileName = configurationSource?.GetString(ConfigurationKeys.PluginConfigurationFileName);

            return(TryLoadJsonConfigurationFile(configurationFileName, out jsonConfigurationSource));
        }