Ejemplo n.º 1
0
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            string environment = Environment.GetEnvironmentVariable("AppMode");

            if (String.IsNullOrWhiteSpace(environment))
            {
                environment = "Production";
            }

            string currentDirectory = Directory.GetCurrentDirectory();
            var    config           = new ConfigurationBuilder()
                                      .SetBasePath(currentDirectory)
                                      .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
                                      .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true)
                                      .AddEnvironmentVariables()
                                      .AddCommandLine(args)
                                      .Build();

            var settings = Settings.ReadFromConfiguration(config, environment);

            var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config);

            if (!String.IsNullOrEmpty(settings.ExceptionlessApiKey))
            {
                loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Verbose);
            }

            Log.Logger = loggerConfig.CreateLogger();

            Log.Information("Bootstrapping {AppMode} mode API ({InformationalVersion}) on {MachineName} using {@Settings} loaded from {Folder}", environment, Settings.Current.InformationalVersion, Environment.MachineName, Settings.Current, currentDirectory);

            bool useApplicationInsights = !String.IsNullOrEmpty(Settings.Current.ApplicationInsightsKey);

            var builder = WebHost.CreateDefaultBuilder(args)
                          .UseEnvironment(environment)
                          .UseKestrel(c => {
                c.AddServerHeader = false;
                if (Settings.Current.MaximumEventPostSize > 0)
                {
                    c.Limits.MaxRequestBodySize = Settings.Current.MaximumEventPostSize;
                }
            })
                          .UseSerilog(Log.Logger)
                          .SuppressStatusMessages(true)
                          .UseConfiguration(config)
                          .ConfigureServices(s => {
                if (useApplicationInsights)
                {
                    s.AddSingleton <ITelemetryInitializer, ExceptionlessTelemetryInitializer>();
                    s.AddHttpContextAccessor();
                    s.AddApplicationInsightsTelemetry();
                }
                s.AddSingleton(settings);
            })
                          .UseStartup <Startup>();

            if (useApplicationInsights)
            {
                builder.UseApplicationInsights(Settings.Current.ApplicationInsightsKey);
            }

            if (settings.EnableMetricsReporting)
            {
                settings.MetricsConnectionString = MetricsConnectionString.Parse(settings.MetricsConnectionString?.ConnectionString);
                ConfigureMetricsReporting(builder);
            }

            return(builder);
        }
Ejemplo n.º 2
0
        public static IServiceProvider GetServiceProvider()
        {
            AppDomain.CurrentDomain.SetDataDirectory();

            string environment = Environment.GetEnvironmentVariable("AppMode");

            if (String.IsNullOrWhiteSpace(environment))
            {
                environment = "Production";
            }

            string currentDirectory = AppContext.BaseDirectory;
            var    config           = new ConfigurationBuilder()
                                      .SetBasePath(currentDirectory)
                                      .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true)
                                      .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true)
                                      .AddEnvironmentVariables()
                                      .Build();

            var settings = Settings.ReadFromConfiguration(config, environment);

            settings.DisableIndexConfiguration = true;

            var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config);

            if (!String.IsNullOrEmpty(Settings.Current.ExceptionlessApiKey) && !String.IsNullOrEmpty(Settings.Current.ExceptionlessServerUrl))
            {
                var client = ExceptionlessClient.Default;
                client.Configuration.SetDefaultMinLogLevel(LogLevel.Warn);
                client.Configuration.UseLogger(new SelfLogLogger());
                client.Configuration.SetVersion(Settings.Current.Version);
                client.Configuration.UseInMemoryStorage();

                if (String.IsNullOrEmpty(Settings.Current.InternalProjectId))
                {
                    client.Configuration.Enabled = false;
                }

                client.Configuration.ServerUrl = Settings.Current.ExceptionlessServerUrl;
                client.Startup(Settings.Current.ExceptionlessApiKey);

                loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Verbose);
            }

            Log.Logger = loggerConfig.CreateLogger();
            Log.Information("Bootstrapping {AppMode} mode job ({InformationalVersion}) on {MachineName} using {@Settings} loaded from {Folder}", environment, Settings.Current.InformationalVersion, Environment.MachineName, Settings.Current, currentDirectory);

            if (settings.EnableMetricsReporting)
            {
                settings.MetricsConnectionString = MetricsConnectionString.Parse(settings.MetricsConnectionString?.ConnectionString);
            }

            var services = new ServiceCollection();

            services.AddLogging(b => b.AddSerilog(Log.Logger));
            services.AddSingleton(settings);
            Core.Bootstrapper.RegisterServices(services);
            Bootstrapper.RegisterServices(services, true);

            var container = services.BuildServiceProvider();

            Core.Bootstrapper.LogConfiguration(container, settings, container.GetRequiredService <ILoggerFactory>());
            if (Settings.Current.EnableBootstrapStartupActions)
            {
                container.RunStartupActionsAsync().GetAwaiter().GetResult();
            }

            return(container);
        }