private static void CreateDbIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <SellersContext>(); DBSeed.Initialize(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } }
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; } }
public static void Main(string[] args) { try { var host = CreateHostBuilder(args).Build(); using (IServiceScope scope = host.Services.CreateScope()) { //using MS.WebCore.Logger //添加以上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; } }