Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();

            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var myContext = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(myContext, loggerFactory).Wait();
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "Error occured seeding the Database.");
                }
            }

            host.Run();
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            //配置Serilog
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug() //最小日志输出级别 Debug
                                               //
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.Console() //输出到控制台
                                            //输出到文件。每天创建一个
                         .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var myContext = services.GetRequiredService <MyContext>();//MyContext在CreateWebHostBuilder的UseStartup这步就已注入到了容器了 所以这里能够这样使用
                    MyContextSeed.SeedAsync(myContext, loggerFactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "Error occured seeding the Database");
                    //Console.WriteLine(ex);
                    //throw;
                }
            }
            host.Run();
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();

            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var service       = scope.ServiceProvider;
                var loggerFactory = service.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = service.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(context, loggerFactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "Errors occured seeding the database");
                }
            }

            host.Run();
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            #region Serilog配置
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day)
                         .CreateLogger();
            #endregion

            IWebHost host = CreateWebHostBuilder(args).Build();
            #region 初始化数据库 不是必须的
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var myContext = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(myContext, loggerFactory).Wait();
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "初始化数据库失败");
                }
            }
            #endregion
            host.Run();
        }
Ejemplo n.º 5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            MyContextSeed.SeedAsync(app, loggerFactory).Wait();
        }
Ejemplo n.º 6
0
        public static void Main(string[] args)
        {
            //CreateWebHostBuilder(args).Build().Run();

            Console.Title = "MY Restful Api";

            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var salesContext = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(salesContext, loggerFactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error");
                }
            }

            host.Run();
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var dbContext = scope.ServiceProvider.GetService <MyContext>();

                    // dbContext.Database.EnsureDeleted();

                    MyContextSeed.SeedAsync(dbContext, loggerFactory).Wait();
                    // dbContext.Database.Migrate();
                }
                catch (Exception e)
                {
                    // var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "Database Migration Error!");
                }
            }
            host.Run();
        }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services  = scope.ServiceProvider;
                var myContext = services.GetRequiredService <MyContext>();
                MyContextSeed.SeedAsync(myContext).Wait();
            }

            host.Run();
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var salesContext = services.GetRequiredService <TvContext>();
                    MyContextSeed.SeedAsync(salesContext, loggerFactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }
Ejemplo n.º 10
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services   = scope.ServiceProvider;
                var logfactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(context, logfactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = logfactory.CreateLogger <Program>();
                    logger.LogError($"Db seed has error occured:{ex.Message}\r\n{ex.StackTrace}");
                }
            }
            host.Run();
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var myContext = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(myContext, loggerFactory).Wait();
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "Error occured seeding The Database");
                }
            }
            host.Run();
        }
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            #region 使用serolog,替换默认
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File(Path.Combine("logs", @"log.txt"),
                                       rollingInterval: RollingInterval.Day,//一天创建一个
                                       rollOnFileSizeLimit: true)
                         .CreateLogger();
            #endregion

            var host = CreateWebHostBuilder(args).Build();

            //数据初始化
            using (var scope = host.Services.CreateScope())
            {
                var service       = scope.ServiceProvider;
                var LoggerFactory = service.GetRequiredService <ILoggerFactory>();

                try
                {
                    var myContext = service.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(myContext, LoggerFactory).Wait();
                }
                catch (Exception e)
                {
                    var logger = LoggerFactory.CreateLogger <Program>();

                    logger.LogError(e, "Error occurd seeding the database");
                }
            }


            host.Run();
        }
Ejemplo n.º 13
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Seq("http://localhost:5341")
                         .CreateLogger();
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var mycontext = services.GetRequiredService <MyContext>();
                    MyContextSeed.SeedAsync(mycontext, loggerFactory).Wait();
                }
                catch (Exception e)
                {
                    throw;
                }
                host.Run();
            }
        }
Ejemplo n.º 14
0
        public static void Main(string[] args)
        {
            //serilog配置
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()                                          //最小日志输入级别
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information) //覆盖带有Microsoft的日志级别为Information
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         //.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day) //记录到文件,每天记录
                         .CreateLogger();


            var host = CreateWebHostBuilder(args).Build();

            #region 执行数据库初始化Seed
            using (var scope = host.Services.CreateScope())                         //获取服务的Scope
            {
                var services      = scope.ServiceProvider;                          //通过Scope获取服务提供商
                var loggerFactory = services.GetRequiredService <ILoggerFactory>(); //通过服务提供商获取日志工厂

                try
                {
                    var myContext = services.GetRequiredService <MyContext>(); //通过服务提供商获取MyContext
                    MyContextSeed.SeedAsync(myContext, loggerFactory).Wait();  //执行数据库Seed种子
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>(); //使用日志工厂创建一个Program级别的日志记录实例
                    logger.LogError(e, "在数据库初始化Seed时发生了错误");             //记录错误日志
                }
            }
            #endregion


            host.Run();
        }