Ejemplo n.º 1
0
        public sealed override async Task <int> ExecuteAsync(CommandContext context, TSettings settings)
        {
            // Set verbose tracing
            if (settings.LogLevel != LogLevel.Information)
            {
                _serviceCollection.Configure <LoggerFilterOptions>(options => options.MinLevel = settings.LogLevel);
            }

            // File logging
            if (!string.IsNullOrEmpty(settings.LogFile))
            {
                // Add the log provider (adding it to the service collection will get picked up by the logger factory)
                _serviceCollection.AddSingleton <ILoggerProvider, FileLoggerProvider>();
                _serviceCollection.Configure <FileLoggerOptions>(options =>
                {
                    options.FileName     = settings.LogFile;
                    options.LogDirectory = string.Empty;
                });
            }

            // Build a temporary service provider so we can log
            IServiceProvider services = _serviceCollection.BuildServiceProvider();

            // Log pending messages
            ILogger logger = services.GetRequiredService <ILogger <Bootstrapper> >();

            logger.LogInformation($"Statiq version {Engine.Version}");
            ClassCatalog classCatalog = services.GetService <ClassCatalog>();

            if (classCatalog != null)
            {
                classCatalog.LogDebugMessages(logger);
            }

            // Attach
            if (settings.Attach)
            {
                logger.LogInformation($"Waiting for a debugger to attach to process {Process.GetCurrentProcess().Id} (or press a key to continue)...");
                while (!Debugger.IsAttached && !Console.KeyAvailable)
                {
                    Thread.Sleep(100);
                }
                if (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                    logger.LogInformation("Key pressed, continuing execution");
                }
                else
                {
                    logger.LogInformation("Debugger attached, continuing execution");
                }
            }

            return(await ExecuteCommandAsync(_serviceCollection, context, settings));
        }
Ejemplo n.º 2
0
        public sealed override async Task <int> ExecuteAsync(CommandContext context, TSettings commandSettings)
        {
            // Set verbose tracing
            if (commandSettings.LogLevel != LogLevel.Information)
            {
                ServiceCollection.Configure <LoggerFilterOptions>(options => options.MinLevel = commandSettings.LogLevel);
            }

            // File logging
            if (!string.IsNullOrEmpty(commandSettings.LogFile))
            {
                // Add the log provider (adding it to the service collection will get picked up by the logger factory)
                ServiceCollection.AddSingleton <ILoggerProvider, FileLoggerProvider>();
                ServiceCollection.Configure <FileLoggerOptions>(options =>
                {
                    options.FileName     = commandSettings.LogFile;
                    options.LogDirectory = string.Empty;
                });
            }

            // Build a temporary service provider so we can log
            // Make sure to place it in it's own scope so transient services get correctly disposed
            IServiceProvider services     = ServiceCollection.BuildServiceProvider();
            ClassCatalog     classCatalog = services.GetService <ClassCatalog>();

            using (IServiceScope serviceScope = services.CreateScope())
            {
                // Log pending messages
                ILogger logger = serviceScope.ServiceProvider.GetRequiredService <ILogger <Bootstrapper> >();
                logger.LogInformation($"Statiq version {Engine.Version}");
                classCatalog?.LogDebugMessages(logger);

                // Attach
                if (commandSettings.Attach)
                {
                    logger.LogInformation($"Waiting for a debugger to attach to process {Process.GetCurrentProcess().Id} (or press a key to continue)...");
                    while (!Debugger.IsAttached && !Console.KeyAvailable)
                    {
                        Thread.Sleep(100);
                    }
                    if (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                        logger.LogInformation("Key pressed, continuing execution");
                    }
                    else
                    {
                        logger.LogInformation("Debugger attached, continuing execution");
                    }
                }
            }

            // Add settings
            if (commandSettings.Settings?.Length > 0)
            {
                foreach (KeyValuePair <string, string> setting in SettingsParser.Parse(commandSettings.Settings))
                {
                    ConfigurationSettings[setting.Key] = setting.Value;
                }
            }

            // Configure settings after other configuration so they can use the values
            ConfigurableSettings configurableSettings = new ConfigurableSettings(ConfigurationSettings);

            Configurators.Configure(configurableSettings);

            return(await ExecuteCommandAsync(context, commandSettings));
        }