From ee92564bf2fc3791d7e95ef4edb9f4e6ecf16aa4 Mon Sep 17 00:00:00 2001 From: Shuozhe Date: Fri, 4 Jun 2021 02:43:16 +0200 Subject: [PATCH] added logging --- {DataAccess => Data}/DataAccessContext.cs | 4 +-- Data/DemoData.cs | 39 +++++++++++++++++++++++ Program.cs | 29 ++++++++++++++++- ProjectGrid.csproj | 3 ++ Startup.cs | 31 +++++++++--------- 5 files changed, 87 insertions(+), 19 deletions(-) rename {DataAccess => Data}/DataAccessContext.cs (72%) create mode 100644 Data/DemoData.cs diff --git a/DataAccess/DataAccessContext.cs b/Data/DataAccessContext.cs similarity index 72% rename from DataAccess/DataAccessContext.cs rename to Data/DataAccessContext.cs index 06dc039..56bef78 100644 --- a/DataAccess/DataAccessContext.cs +++ b/Data/DataAccessContext.cs @@ -6,10 +6,10 @@ using ProjectGrid.Models; namespace ProjectGrid.Data { - class DataAccessContex : DbContext + public class DataAccessContext : DbContext { - public DataAccessContex(DbContextOptions options) { } + public DataAccessContext(DbContextOptions options) { } public DbSet Users; diff --git a/Data/DemoData.cs b/Data/DemoData.cs new file mode 100644 index 0000000..473a72d --- /dev/null +++ b/Data/DemoData.cs @@ -0,0 +1,39 @@ +using ProjectGrid.Models; +using System; +using System.Linq; + +namespace ProjectGrid.Data +{ + public static class DbInitializer + { + + public static void Initialize(DataAccessContext context) + { + context.Database.EnsureCreated(); + + // Look for any UserModels. + if (context.Users.Any()) + { + return; // DB has been seeded + } + + var users = new UserModel[] + { + new UserModel{firstName="Carson",lastName="Alexander",email="test@abc.com"}, + new UserModel{firstName="Meredith",lastName="Alonso",email="test@abc.com"}, + new UserModel{firstName="Arturo",lastName="Anand",email="test@abc.com"}, + new UserModel{firstName="Gytis",lastName="Barzdukas",email="test@abc.com"}, + new UserModel{firstName="Yan",lastName="Li",email="test@abc.com"}, + new UserModel{firstName="Peggy",lastName="Justice",email="test@abc.com"}, + new UserModel{firstName="Laura",lastName="Norman",email="test@abc.com"}, + new UserModel{firstName="Nino",lastName="Olivetto",email="test@abc.com"} + }; + foreach (UserModel s in users) + { + context.Users.Add(s); + } + context.SaveChanges(); + + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index eabbde1..792af80 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,5 @@ +using ProjectGrid.Data; +using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; @@ -11,13 +13,38 @@ namespace ProjectGrid { public class Program { + + private static ILogger _logger; public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + + using (var scope = host.Services.CreateScope()) + { + var services = scope.ServiceProvider; + _logger = services.GetRequiredService>(); + try + { + var context = services.GetRequiredService(); + DbInitializer.Initialize(context); + } + catch (Exception ex) + { + + _logger.LogError(ex, "An error occurred while seeding the database."); + } + } + + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .ConfigureLogging(loggingBuilder => + { + loggingBuilder.ClearProviders(); + loggingBuilder.AddDebug().AddEventLog(); + }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/ProjectGrid.csproj b/ProjectGrid.csproj index e964942..c3c6e97 100644 --- a/ProjectGrid.csproj +++ b/ProjectGrid.csproj @@ -10,6 +10,9 @@ + + + diff --git a/Startup.cs b/Startup.cs index 721cfe9..5428efb 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,15 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.ApplicationInsights; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Microsoft.EntityFrameworkCore; using ProjectGrid.Data; @@ -18,22 +17,21 @@ namespace ProjectGrid { public class Startup { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } + ILogger _logger; + + public Startup() { } + public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - - services.AddDbContext(options => - options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); - + var connectionString = Configuration.GetConnectionString("DefaultConnection"); + services.AddDbContext(options => options.UseSqlServer(connectionString)); + //_logger.LogInformation($"DataAccessContext registered with {connectionString}"); // TODO: for Testing - services.AddDatabaseDeveloperPageExceptionFilter(); + //services.AddDatabaseDeveloperPageExceptionFilter(); services.AddControllers(); services.AddSwaggerGen(c => { @@ -42,8 +40,9 @@ namespace ProjectGrid } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) { + _logger = logger; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();