using EnvelopeGenerator.DependencyInjection; using EnvelopeGenerator.Finalizer; using EnvelopeGenerator.Infrastructure; using Microsoft.EntityFrameworkCore; using Serilog; // Load Serilog from appsettings.json Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build()) .CreateLogger(); try { var builder = Host.CreateApplicationBuilder(args); // add serilog builder.Logging.ClearProviders(); builder.Logging.AddSerilog(); var config = builder.Configuration; builder.Services.AddHostedService(); #region Add DB Context, EG Inf. and Services var cnnStrName = "Default"; var connStr = config.GetConnectionString(cnnStrName) ?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration."); builder.Services.AddEnvelopeGenerator(egOptions => egOptions .AddLocalization() .AddDistributedSqlServerCache(options => { options.ConnectionString = connStr; options.SchemaName = "dbo"; options.TableName = "TBDD_CACHE"; }) .AddInfrastructure(opt => { opt.AddDbTriggerParams(config); opt.AddDbContext((provider, options) => { var logger = provider.GetRequiredService>(); options.UseSqlServer(connStr) .LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace) .EnableSensitiveDataLogging() .EnableDetailedErrors(); }); }) .AddServices(config) ); #endregion Add DB Context, EG Inf. and Services var host = builder.Build(); host.Run(); Log.Information("The worker was stopped."); } catch(Exception ex) { Log.Fatal(ex, "Worker could not be started!"); } finally { Log.CloseAndFlush(); }