Esempio n. 1
0
        private static async Task <int> MainMicrosoft(bool commandLine)
        {
            try
            {
                TerminalConsole.Initialise(commandLine);

                var Builder = new HostBuilder()
                              .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);

                    config.AddEnvironmentVariables();
                })
                              .ConfigureLogging((hostContext, logging) =>
                {
                    logging.AddTerminal(TerminalConsole.View, registry =>
                    {
                        // Load the Program type and Standard Commands
                        registry.Scan(typeof(Program));
                        registry.Scan(typeof(StandardCommands));
                    });
                })
                              .ConfigureServices((hostContext, services) =>
                {
                    var Configuration = hostContext.Configuration;

                    services.AddSingleton <Program>();
                    services.AddSingleton <IHostedService>(provider => provider.GetRequiredService <Program>());
                })
                              .UseConsoleLifetime()
                ;

                var Host = Builder.Build();

                await Host.RunAsync(Exited.Token);

                return(0);
            }
            finally
            {
                TerminalConsole.Terminate();
            }
        }
Esempio n. 2
0
        private static async Task <int> MainSerilog(bool commandLine)
        {
            try
            {
                TerminalConsole.Initialise(commandLine);

                var Builder = new HostBuilder()
                              .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);

                    config.AddEnvironmentVariables();
                })
                              .ConfigureLogging((hostContext, logging) =>
                {
                    var LoggerConfig = new LoggerConfiguration()
                                       .MinimumLevel.Is(LogEventLevel.Verbose)
                                       .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                                       .MinimumLevel.Override("System", LogEventLevel.Warning)
                                       .Enrich.FromLogContext()
                                       .WriteTo.Terminal(TerminalConsole.View)                  // All log entries route to the console
                    ;

                    Log.Logger = LoggerConfig.CreateLogger();

                    logging.AddSerilog();
                })
                              .ConfigureServices((hostContext, services) =>
                {
                    var Configuration = hostContext.Configuration;

                    // Since we didn't register the Terminal during ConfigureLogging, we need to register it here
                    services.AddTerminal(registry =>
                    {
                        // Load the Program type and Standard Commands
                        registry.Scan(typeof(Program));
                        registry.Scan(typeof(StandardCommands));
                    });
                    services.AddSingleton <Program>();
                    services.AddSingleton <IHostedService>(provider => provider.GetRequiredService <Program>());
                })
                              .UseSerilog()
                              .UseConsoleLifetime()
                ;

                var Host = Builder.Build();

                await Host.RunAsync(Exited.Token);

                return(0);
            }
            catch (OperationCanceledException)
            {
                return(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Log.Logger.Fatal(e, "Host terminated unexpectedly");

                return(-1);
            }
            finally
            {
                Log.CloseAndFlush();

                TerminalConsole.Terminate();
            }
        }