private static void ConfigureServices(string hostHttpsUrl) { ServiceCollection services = new ServiceCollection(); services.AddLogging(logger => { logger .AddConsole(options => options.FormatterName = nameof(PlainTextConsoleFormatter)) .AddConsoleFormatter <PlainTextConsoleFormatter, ConsoleFormatterOptions>() .AddFile($"Results/DWABench_execution_{executionTime.ToString("yyyyMMdd_HHmmss")}.txt", options => { options.FormatLogEntry = (msg) => { if (msg.LogLevel >= LogLevel.Information) //in execution results file we only want to have information and higher levels { return(msg.Message); } return(string.Empty); }; }) .AddFile($"Logs/{executionTime.ToString("yyyyMMdd_HHmmss")}.txt") .AddFilter("Microsoft.*", LogLevel.Error) .AddFilter("System.*", LogLevel.Error); }); services.AddDotnetWebApiBenchApiRefitClients(hostHttpsUrl, true); services.AddSingleton <TestDataGenerator>(); services.AddSingleton <Phase1Scenario>(); services.AddSingleton <Phase2Scenario>(); services.AddSingleton <PlainTextConsoleFormatter>(); services.AddScoped <BenchmarkRunner>(); services.AddSingleton <IConfiguration>(ctx => Configuration); Container = services.BuildServiceProvider(); logger = Container.GetRequiredService <ILogger <Program> >(); }
/// <summary> /// Build the web host /// </summary> /// <param name="kvUrl">URL of the Key Vault</param> /// <param name="authType">MI, CLI, VS</param> /// <returns>Web Host ready to run</returns> private static async Task <IWebHost> BuildHost(string kvUrl, AuthenticationType authType) { // create the Key Vault Client KeyVaultClient kvClient = await KeyVaultHelper.GetKeyVaultClient(kvUrl, authType, Constants.CosmosDatabase).ConfigureAwait(false); if (kvClient == null) { return(null); } // build the config // we need the key vault values for the DAL config = BuildConfig(kvClient, kvUrl); // configure the web host builder IWebHostBuilder builder = WebHost.CreateDefaultBuilder() .UseConfiguration(config) .UseUrls(string.Format(System.Globalization.CultureInfo.InvariantCulture, $"http://*:{Constants.Port}/")) .UseStartup <Startup>() .UseShutdownTimeout(TimeSpan.FromSeconds(Constants.GracefulShutdownTimeout)) .ConfigureServices(services => { // add the data access layer via DI services.AddDal( new Uri(config.GetValue <string>(Constants.CosmosUrl)), config.GetValue <string>(Constants.CosmosKey), config.GetValue <string>(Constants.CosmosDatabase), config.GetValue <string>(Constants.CosmosCollection)); // add the KeyVaultConnection via DI services.AddKeyVaultConnection(kvClient, kvUrl); // add IConfigurationRoot services.AddSingleton <IConfigurationRoot>(config); services.AddKeyRotation(); services.AddResponseCaching(); }); // configure logger based on command line builder.ConfigureLogging(logger => { logger.ClearProviders(); logger.AddConsole(); // if you specify the --log-level option, it will override the appsettings.json options // remove any or all of the code below that you don't want to override if (App.IsLogLevelSet) { logger.AddFilter("Microsoft", AppLogLevel) .AddFilter("System", AppLogLevel) .AddFilter("Default", AppLogLevel) .AddFilter("CSE.Helium", AppLogLevel); } }); // build the host return(builder.Build()); }
/// <summary> /// Build the web host /// </summary> /// <param name="kvUrl">URL of the Key Vault</param> /// <param name="authType">MSI, CLI, VS</param> /// <returns>Web Host ready to run</returns> static async Task <IWebHost> BuildHost(string kvUrl, AuthenticationType authType) { // create the Key Vault Client var kvClient = await GetKeyVaultClient(kvUrl, authType).ConfigureAwait(false); if (kvClient == null) { return(null); } // build the config // we need the key vault values for the DAL config = BuildConfig(kvClient, kvUrl); // configure the web host builder IWebHostBuilder builder = WebHost.CreateDefaultBuilder() .UseConfiguration(config) .UseKestrel() .UseUrls(string.Format(System.Globalization.CultureInfo.InvariantCulture, $"http://*:{Constants.Port}/")) .UseStartup <Startup>() .ConfigureServices(services => { // add the data access layer via DI services.AddDal(new Uri(config.GetValue <string>(Constants.CosmosUrl)), config.GetValue <string>(Constants.CosmosKey), config.GetValue <string>(Constants.CosmosDatabase), config.GetValue <string>(Constants.CosmosCollection)); // add the KeyVaultConnection via DI services.AddKeyVaultConnection(kvClient, new Uri(kvUrl)); // add IConfigurationRoot services.AddSingleton <IConfigurationRoot>(config); services.AddResponseCaching(); }) // configure logger based on command line .ConfigureLogging(logger => { logger.ClearProviders(); logger.AddConsole() .AddFilter("Microsoft", HeliumLogLevel) .AddFilter("System", HeliumLogLevel) .AddFilter("Default", HeliumLogLevel); }); // build the host return(builder.Build()); }
/// <summary> /// Build the web host /// </summary> /// <param name="useInMemory">Use in memory DB flag</param> /// <returns>Web Host ready to run</returns> private static IWebHost BuildHost() { // build the config config = BuildConfig(); // configure the web host builder IWebHostBuilder builder = WebHost.CreateDefaultBuilder() .UseConfiguration(config) .UseUrls(string.Format(System.Globalization.CultureInfo.InvariantCulture, $"http://*:{Constants.Port}/")) .UseStartup <Startup>() .UseShutdownTimeout(TimeSpan.FromSeconds(Constants.GracefulShutdownTimeout)) .ConfigureServices(services => { // add the data access layer via DI if (App.Secrets.UseInMemoryDb) { services.AddInMemoryDal(); } else { services.AddDal(new Uri(Secrets.CosmosServer), Secrets.CosmosKey, Secrets.CosmosDatabase, Secrets.CosmosCollection); } // add IConfigurationRoot services.AddSingleton <IConfigurationRoot>(config); services.AddResponseCaching(); }); // configure logger based on command line builder.ConfigureLogging(logger => { logger.ClearProviders(); logger.AddConsole(); // if you specify the --log-level option, it will override the appsettings.json options // remove any or all of the code below that you don't want to override if (App.IsLogLevelSet) { logger.AddFilter("Microsoft", AppLogLevel) .AddFilter("System", AppLogLevel) .AddFilter("Default", AppLogLevel) .AddFilter("CSE.NextGenSymmetricApp", AppLogLevel); } }); // build the host return(builder.Build()); }
public static int Main(string[] args) { // Create servicecollection with di services var services = new ServiceCollection() .AddSingleton <Lib.Interfaces.IAppConfiguration, Lib.Services.JsonFileConfiguration>() .AddSingleton <Lib.Interfaces.IInstanceService, Lib.Services.MastodonInstanceService>() .AddSingleton <Lib.Interfaces.IFeedService, Lib.Services.FeedService>() .AddSingleton <Lib.Interfaces.ICacheService, Lib.Services.SqliteCache.SqliteCacheService>() .AddSingleton <IConsole>(PhysicalConsole.Singleton) .AddLogging(log => { // Add console logger log.AddConsole(c => c.IncludeScopes = true); log.AddSerilog(); if (Program.Debug) { log.SetMinimumLevel(LogLevel.Debug); } }) .BuildServiceProvider(); var cfg = services.GetService <IAppConfiguration>(); // Initialize serilog logger Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(cfg.ConfigurationRoot) .Enrich.WithEnvironmentUserName() .Enrich.WithMachineName() .Enrich.FromLogContext() .CreateLogger(); // Create application for configuration var app = new CommandLineApplication <Program>(); // configure app app.Conventions .UseDefaultConventions() .UseConstructorInjection(services); // run and return var result = app.Execute(args); services.Dispose(); return(result); }