static async Task MainAsync() { Console.CancelKeyPress += (_, e) => { if (!_cts.IsCancellationRequested) { _cts.Cancel(); } e.Cancel = true; }; var cfile = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "Config.json")); if (!cfile.Exists) { using (var fs = cfile.CreateText()) { fs.WriteLine(JsonConvert.SerializeObject(LaveyConfig.Default, Formatting.Indented)); fs.Flush(); } _log.Info("Default configuration file created."); Environment.Exit(1); } LaveyConfig config = default; using (var fs = cfile.OpenText()) config = JsonConvert.DeserializeObject <LaveyConfig>(fs.ReadToEnd()); NLogExtensions.InstallSeq(config); await SetupShardsAsync(config); foreach (var bot in GetShards()) { await bot.InitializeAsync().ConfigureAwait(false); } while (!_cts.IsCancellationRequested) { await Task.Delay(100); } foreach (var bot in GetShards()) { await bot.ShutdownAsync(); } }
public static void Main(string[] args) { Logger logger = LogManager.GetCurrentClassLogger(); try { var host = CreateHostBuilder(args).Build(); logger.Trace("网站启动中..."); using (IServiceScope scope = host.Services.CreateScope()) { logger.Trace("初始化NLog"); //确保NLog.config中连接字符串与appsettings.json中同步 NLogExtensions.EnsureNlogConfig("NLog.config", "mssql", scope.ServiceProvider.GetRequiredService <IConfiguration>().GetSection("ConnectionStrings:PHDbContext").Value); logger.Trace("初始化数据库"); //初始化数据库 DBSeed.Initialize(scope.ServiceProvider.GetRequiredService <IUnitOfWork <PHDbContext> >()); //for test -start //用于查看彩色控制台样式,以及日志等级过滤 /*logger.Trace("Test For Trace"); * logger.Debug("Test For Debug"); * logger.Info("Test For Info"); * logger.Warn("Test For Warn"); * logger.Error("Test For Error"); * logger.Fatal("Test For Fatal");*/ //for test -end } logger.Trace("网站启动完成"); host.Run(); } catch (Exception ex) { logger.Fatal(ex, "网站启动失败"); throw; } }
private static async Task MainLogic(CancellationToken ct) { // Main logic here await DA.InitializeDB(); await DA.AddAnotherJalluToDBTest(); NLogExtensions.InitializeLogConnection(ct, IPAddress.Parse("127.0.0.1"), 9999); var serviceLayer = new CommService(); serviceLayer.Run(ct); var delayTask = Task.Delay(-1, ct); // Test. var bll = new BusinessLogic(serviceLayer, new RobotCell(), new DA()); var done = await Task.WhenAny(delayTask); ct.ThrowIfCancellationRequested(); }
public static void Main(string[] args) { try { var host = CreateHostBuilder(args).Build(); using (IServiceScope scope = host.Services.CreateScope()) { //添加以上using引用 //确保NLog.config中连接字符串与appsettings.json中同步 NLogExtensions.EnsureNlogConfig("NLog.config", "MySQL", scope.ServiceProvider.GetRequiredService <IConfiguration>().GetSection("ConectionStrings:MSDbContext").Value); //初始化数据库 DBSeed.Initialize(scope.ServiceProvider.GetRequiredService <IUnitOfWork <MSDbContext> >()); } host.Run(); } catch (Exception ex) { throw; } }
static void Main(string[] args) { NLogExtensions.Install(); try { if (_mutex.WaitOne(0, false)) { MainAsync().GetAwaiter().GetResult(); } else { throw new InvalidOperationException("Lavey bot already running."); } } catch (Exception ex) { _log.Error(ex.ToString()); Console.ReadLine(); Environment.Exit(-1); } }
public NLogMonitor() { NLogExtensions.Init(); }