コード例 #1
0
ファイル: NaosExtensions.cs プロジェクト: vip32/Naos
        /// <summary>
        /// Adds required services to support the Discovery functionality.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="product"></param>
        /// <param name="capability"></param>
        /// <param name="tags"></param>
        /// <param name="optionsAction"></param>
        /// <param name="environment"></param>
        /// <param name="section"></param>
        public static INaosBuilderContext AddNaos(
            this IServiceCollection services,
            string product    = null,
            string capability = null,
            string[] tags     = null,
            Action <NaosServicesContextOptions> optionsAction = null,
            string environment = null,
            string section     = "naos")
        {
            Console.WriteLine("--- naos service start", System.Drawing.Color.LimeGreen);
            EnsureArg.IsNotNull(services, nameof(services));

            //using var serviceProvider = services.BuildServiceProvider();
            var configuration = /*serviceProvider.GetService<IConfiguration>() ??*/ NaosConfigurationFactory.Create();

            services.AddSingleton(configuration);
            services.AddMediatr();
            services.Configure <ConsoleLifetimeOptions>(opts => opts.SuppressStatusMessages = true); // https://andrewlock.net/new-in-aspnetcore-3-structured-logging-for-startup-messages/

            var naosConfiguration = configuration?.GetSection(section).Get <NaosConfiguration>();
            var context           = new NaosBuilderContext
            {
                Services      = services,
                Configuration = configuration,
                Environment   = environment ?? Environment.GetEnvironmentVariable(EnvironmentKeys.Environment) ?? "Production",
                Descriptor    = new Naos.Foundation.ServiceDescriptor(
                    product ?? naosConfiguration.Product,
                    capability ?? naosConfiguration.Product,
                    tags: tags ?? naosConfiguration.Tags),
            };

            foreach (var provider in configuration?.Providers.Safe())
            {
                context.Messages.Add($"naos services builder: configuration provider added (type={provider.GetType().Name})");
            }

            context.Messages.Add("naos services builder: naos services added");
            //context.Services.AddSingleton(new NaosFeatureInformation { Name = "Naos", EchoRoute = "naos/servicecontext/echo" });

            // TODO: optional or provide own settings?
            JsonConvert.DefaultSettings = DefaultJsonSerializerSettings.Create;

            optionsAction?.Invoke(new NaosServicesContextOptions(context));

            AddConfigurationHealthChecks(services, configuration);
            LogStartupMessages(services, context);

            return(context);
        }
コード例 #2
0
ファイル: NaosExtensions.cs プロジェクト: vip32/Naos
 private static void LogStartupMessages(IServiceCollection services, NaosBuilderContext context)
 {
     try
     {
         var logger = services.BuildServiceProvider().GetService <ILoggerFactory>().CreateLogger("Naos");
         logger?.LogInformation($"{{LogKey:l}} service descriptor: {context.Descriptor} [{context.Descriptor.Tags.ToString("|")}]", LogKeys.Startup);
         foreach (var message in context.Messages.Safe())
         {
             logger?.LogDebug($"{{LogKey:l}} {message.Replace("{", string.Empty, StringComparison.OrdinalIgnoreCase).Replace("}", string.Empty, StringComparison.OrdinalIgnoreCase):l}", LogKeys.Startup);
         }
     }
     catch (InvalidOperationException)
     {
         // no loggerfactory registered
         services.AddScoped((Func <IServiceProvider, ILoggerFactory>)(sp => new LoggerFactory()));
     }
 }