Example #1
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            var configuration = hostContext.Configuration;
            services.Configure <DiagnosticsMonitorOptions>(options =>
            {
                options.Kubernetes   = string.Equals(bool.TrueString, configuration["kubernetes"], StringComparison.OrdinalIgnoreCase);
                options.AssemblyName = configuration["assemblyName"];
                options.Service      = configuration["service"];

                var section = configuration.GetSection("provider");
                foreach (var child in section.GetChildren())
                {
                    // The value used to connect to the service should be accessible with Tye service discovery.
                    DiagnosticsProvider provider;
                    if (child.Value is string && child.Value.StartsWith("service:"))
                    {
                        var service = child.Value.Substring("service:".Length);
                        var value   = configuration.GetServiceUri(service)?.AbsoluteUri ?? configuration.GetConnectionString(service);
                        provider    = new DiagnosticsProvider(child.Key, value);
                    }
                    else
                    {
                        provider = new DiagnosticsProvider(child.Key, child.Value);
                    }

                    options.Providers.Add(provider);
                }

                if (options.Providers.Count == 0)
                {
                    throw new InvalidOperationException("At least one provider must be configured.");
                }

                if (string.IsNullOrEmpty(options.AssemblyName))
                {
                    throw new InvalidOperationException("The assembly name is required.");
                }

                if (string.IsNullOrEmpty(options.Service))
                {
                    throw new InvalidOperationException("The service name is required.");
                }
            });
            services.AddHostedService <DiagnosticsMonitor>();
        });
Example #2
0
        public static bool TryParse(string text, ProviderKind kind, [MaybeNullWhen(false)] out DiagnosticsProvider provider)
        {
            if (string.IsNullOrEmpty(text))
            {
                provider = null !;
                return(false);
            }

            var pair = text.Split('=');

            if (pair.Length < 2)
            {
                provider = new DiagnosticsProvider(pair[0].Trim().ToLowerInvariant(), null, kind);
                return(true);
            }

            provider = new DiagnosticsProvider(pair[0].Trim().ToLowerInvariant(), pair[1].Trim().ToLowerInvariant(), kind);
            return(true);
        }