Esempio n. 1
0
        private static void RunTraktor(string[] args)
        {
            var logLevelSwitch = new Serilog.Core.LoggingLevelSwitch(Serilog.Events.LogEventLevel.Information);

            Log.Logger = new LoggerConfiguration().MinimumLevel.ControlledBy(logLevelSwitch).WriteTo.Console(outputTemplate: "{Message}{NewLine}").WriteTo.File("Logs/.log", outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day).CreateLogger();

            try
            {
                var config = new ConfigurationBuilder()
                             .SetBasePath(Environment.CurrentDirectory)
                             .AddCommandLine(args)
                             .AddJsonFile("appsettings.json", true, true)
                             .Build();

                Interval  = config.GetValue <TimeSpan?>("interval") ?? TimeSpan.FromMinutes(5);
                KeepAlive = config.GetValue <bool>("keepalive");
                ConnectivityScriptPath = config.GetValue <string>("connectscript");

                var logLevel = config.GetValue <string>("loglevel");
                if (!string.IsNullOrEmpty(logLevel))
                {
                    logLevelSwitch.MinimumLevel = Enum.Parse <Serilog.Events.LogEventLevel>(logLevel);
                }
                else if (System.Diagnostics.Debugger.IsAttached)
                {
                    logLevelSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Debug;
                }

                Log.Information($"Minimum log level = {logLevelSwitch.MinimumLevel}");
                AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
                {
                    LogException((Exception)e.ExceptionObject);
                };

                AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
                {
                    Log.Information("Shutting down ..");
                };

                if (logLevelSwitch.MinimumLevel == Serilog.Events.LogEventLevel.Verbose)
                {
                    Log.Information("Logging all exceptions raised (First Chance), this also includes handled exceptions.");
                    AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
                    {
                        Log.Verbose($"Exception (FC): {e.Exception}");
                    };
                }

                DisplayBindingIp();

                //Console.ReadLine();
                //return;

                Curator.CuratorConfiguration traktorConfig = LoadCuratorConfiguration(config, args);

                TraktService ts      = new TraktService();
                Curator      curator = new Curator(ts);

                if (StartCurator(curator, ts, traktorConfig))
                {
                    if (!string.IsNullOrEmpty(config.GetValue <string>("urls")))
                    {
                        var startup = new Traktor.Web.Startup(config, curator);
                        var host    = Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(x =>
                        {
                            x.ConfigureServices(startup.ConfigureServices).Configure(startup.Configure);
                        }).UseConsoleLifetime().Build().RunAsync();

                        Log.Information($"Running Traktor.Web @ {string.Join(", ", startup.Addresses)}");
                    }

                    appSettingsHash = FileService.ComputeHash("appsettings.json");
                    ChangeToken.OnChange(() => config.GetReloadToken(), () =>
                    {
                        var currentHash = FileService.ComputeHash("appsettings.json");
                        if (currentHash.SequenceEqual(appSettingsHash))
                        {
                            return;
                        }

                        appSettingsHash = FileService.ComputeHash("appsettings.json");
                        curator.UpdateConfiguration(LoadCuratorConfiguration(config, args));

                        Log.Information("Updated configuration! (Changes to Downloader settings requires a restart to take effect)");
                    });

                    ScheduleCuratorUpdates(curator, ts);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw;
            }
        }