public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration) { services.AddDbContext <CadetContext>(options => { options.UseSqlServer(CustomExtensionMethods.GetConnectionString(configuration), sqlServerOptionsAction: sqlOptions => { // sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); } ); return(services); }
// public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); public static int Main(string[] args) { //added to get the config earlier. var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables(); var config = builder.Build(); //not used for now because needed to troubleshoot the mssqlserver connection var connectionString = CustomExtensionMethods.GetConnectionString(config); Log.Logger = new LoggerConfiguration() //.MinimumLevel.Verbose() .MinimumLevel.Debug() // .MinimumLevel.Override("Microsoft", LogEventLevel.Information) //.Enrich.WithProperty("CadetContext", AppName) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.MSSqlServer("Data Source=db;Initial Catalog=cadetapi;User Id=SA;Password=Duuu988@P4w0RD!", "SeriLog", autoCreateSqlTable: true) .WriteTo.Seq("http://seq:5341", apiKey: "bIkNLejR84naps0prwcF") //.ReadFrom.Configuration(config) .CreateLogger(); try { Log.Information("Starting web host ({ApplicationContext})...", AppName); CreateHostBuilder(args).Build().Run(); return(0); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { Log.Fatal(ex, "Host terminated unexpectedly"); return(1); } finally { Log.CloseAndFlush(); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy()) .AddSqlServer(CustomExtensionMethods.GetConnectionString(Configuration), name: "CadetApiDB-check", healthQuery: "Select 1;", tags: new string[] { "cadetapidb" }); services .AddCustomDbContext(Configuration) .AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); }); }