Example #1
0
    /// <inheritdoc />
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (optionsBuilder.IsConfigured)
        {
            base.OnConfiguring(optionsBuilder);
            return;
        }

        string           workingDirectory = AssemblyHelper.GetEntryAssembly().GetDirectoryPath();
        IHostEnvironment environment      = new HostingEnvironment
        {
            EnvironmentName         = EnvironmentHelper.GetEnvironmentName(),
            ApplicationName         = AppDomain.CurrentDomain.FriendlyName,
            ContentRootPath         = workingDirectory,
            ContentRootFileProvider = new PhysicalFileProvider(workingDirectory)
        };

        IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

        configurationBuilder.SetBasePath(workingDirectory);

        IConfiguration configuration = IConfigurationBuilderHelper.CreateConfiguration()
                                       .AddConfigurationFiles(EnvironmentHelper.GetEnvironmentName())
                                       .AddEnvironmentVariables()
                                       .AddUserSecrets()
                                       .Build();
        IConfigurationSection dataSection = configuration.GetSection("data");
        bool enableLogging = dataSection.GetValue <bool>("logging");

        if (enableLogging)
        {
            optionsBuilder.UseLoggerFactory(LogFactoryHelper.ConsoleLoggerFactory)
            .EnableSensitiveDataLogging();
        }

        //optionsBuilder.UseLazyLoadingProxies();
        optionsBuilder.UseSqlite(dataSection.GetConnectionString("DefaultConnection"), e => e.MigrationsAssembly(typeof(DataContext).Assembly.GetName().Name));
        optionsBuilder.EnableDetailedErrors(environment.IsDevelopment());
    }
        /// <inheritdoc />
        protected override void OnStartup(StartupEventArgs e)
        {
            // Setup
            JsonConvert.DefaultSettings = () => JsonHelper.CreateSettings();
            string basePath       = Assembly.GetExecutingAssembly().GetDirectoryPath();
            bool   consoleCreated = false;

            if (e.Args.Length > 0)
            {
                ConsoleHelper.AttachConsole(out consoleCreated);

                if (ConsoleHelper.HasConsole)
                {
                    ConsoleHelper.Show();
                }
                else
                {
                    Logger.LogError("Could not create Writer window.");
                    Shutdown();
                    return;
                }
            }

            // Configuration
            Logger.LogInformation("Loading configuration.");
            IConfiguration configuration = IConfigurationBuilderHelper.CreateConfiguration(basePath)
                                           .AddConfigurationFiles(basePath, EnvironmentHelper.GetEnvironmentName())
                                           .AddEnvironmentVariables()
                                           .AddUserSecrets()
                                           .AddArguments(e.Args)
                                           .Build();

            Parser           parser = null;
            StartupArguments args;

            try
            {
                // Command line
                Logger.LogInformation("Processing command line.");
                parser = new Parser(settings =>
                {
                    settings.CaseSensitive             = false;
                    settings.CaseInsensitiveEnumValues = true;
                });

                ParserResult <StartupArguments> result = parser.ParseArguments <StartupArguments>(e.Args);

                if (result.Tag == ParserResultType.NotParsed)
                {
                    Logger.LogError(StartupArguments.GetUsage(result));
                    Shutdown();
                    return;
                }

                args = ((Parsed <StartupArguments>)result).Value;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.CollectMessages());
                Shutdown();
                return;
            }
            finally
            {
                // in case an error occurred and the parser was not disposed
                ObjectHelper.Dispose(ref parser);
            }

            // Logging
            Logger.LogInformation("Configuring logging.");

            if (configuration.GetValue <bool>("Logging:Enabled") && args.LogLevel < LogLevel.None)
            {
                LoggerConfiguration loggerConfiguration = new();
                loggerConfiguration.ReadFrom.Configuration(configuration);
                loggerConfiguration.MinimumLevel.Is(args.LogLevel switch
                {
                    LogLevel.Trace => LogEventLevel.Verbose,
                    LogLevel.Debug => LogEventLevel.Debug,
                    LogLevel.Information => LogEventLevel.Information,
                    LogLevel.Warning => LogEventLevel.Warning,
                    LogLevel.Error => LogEventLevel.Error,
                    _ => LogEventLevel.Fatal
                });