From cb8965d13234a2d914cc74e0ac738051c9ac0119 Mon Sep 17 00:00:00 2001 From: reidst Date: Sun, 10 Dec 2023 14:28:08 -0600 Subject: [PATCH] Added verbose logs to Program.cs and DbInitializer.cs --- Data/DbInitializer.cs | 42 ++++++++++++++++++++++++++++++++++++++++-- Program.cs | 21 +++++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Data/DbInitializer.cs b/Data/DbInitializer.cs index 8e10f19..5b44f86 100644 --- a/Data/DbInitializer.cs +++ b/Data/DbInitializer.cs @@ -21,16 +21,21 @@ public static class DbInitializer public static void Initialize(ISeeGreenContext context) { string seedDbPath = $"{Directory.GetCurrentDirectory()}/wwwroot/seed.db"; - Console.WriteLine($"seedDbPath='{seedDbPath}'"); + Console.WriteLine($"[DebugLog] seedDbPath='{seedDbPath}'"); using SqliteConnection connection = new($"Data Source={seedDbPath}"); + Console.WriteLine("[DebugLog] connecting to SQLite seed database..."); connection.Open(); - + Console.WriteLine("[DebugLog] Connection opened."); + if (!context.Categories.Any()) { + Console.WriteLine("[DebugLog] Table 'Categories' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Categories;"; List categories = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { categories.Add(new Categories @@ -41,16 +46,22 @@ public static void Initialize(ISeeGreenContext context) APG4sort = DbCast(reader["APG4sort"]), }); } + Console.WriteLine($"[DebugLog] Adding {categories.Count} new Categories to the context..."); context.Categories.AddRange(categories.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } if (!context.TaxonomicOrders.Any()) { + Console.WriteLine("[DebugLog] Table 'TaxonomicOrders' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM TaxonomicOrders;"; List taxonomicOrders = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { taxonomicOrders.Add(new TaxonomicOrders @@ -70,16 +81,22 @@ public static void Initialize(ISeeGreenContext context) SortLevel6 = (int)DbCast(reader["SortLevel6"]), }); } + Console.WriteLine($"[DebugLog] Adding {taxonomicOrders.Count} new TaxonomicOrders to the context..."); context.TaxonomicOrders.AddRange(taxonomicOrders.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } if (!context.Families.Any()) { + Console.WriteLine("[DebugLog] Table 'Families' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Families;"; List families = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { families.Add(new Families @@ -90,16 +107,22 @@ public static void Initialize(ISeeGreenContext context) TaxonomicOrderID = DbCast(reader["TaxonomicOrderID"]), }); } + Console.WriteLine($"[DebugLog] Adding {families.Count} new Families to the context..."); context.Families.AddRange(families.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } if (!context.Genera.Any()) { + Console.WriteLine("[DebugLog] Table 'Genera' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Genera;"; List genera = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { genera.Add(new Genera @@ -109,16 +132,22 @@ public static void Initialize(ISeeGreenContext context) FamilyID = DbCast(reader["FamilyID"]), }); } + Console.WriteLine($"[DebugLog] Adding {genera.Count} new Genera to the context..."); context.Genera.AddRange(genera.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } if (!context.Taxa.Any()) { + Console.WriteLine("[DebugLog] Table 'Taxa' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Taxa;"; List taxa = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { taxa.Add(new Taxa @@ -135,16 +164,22 @@ public static void Initialize(ISeeGreenContext context) USDAsynonym = DbCast(reader["USDAsynonym"]), }); } + Console.WriteLine($"[DebugLog] Adding {taxa.Count} new Taxa to the context..."); context.Taxa.AddRange(taxa.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } if (!context.Synonyms.Any()) { + Console.WriteLine("[DebugLog] Table 'Synonyms' is empty, adding seed data..."); SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Synonyms;"; List synonyms = new(); + Console.WriteLine("[DebugLog] Executing SELECT command..."); using SqliteDataReader reader = command.ExecuteReader(); + Console.WriteLine("[DebugLog] Reading data..."); while (reader.Read()) { synonyms.Add(new Synonyms @@ -158,8 +193,11 @@ public static void Initialize(ISeeGreenContext context) Authors = DbCast(reader["Authors"]), }); } + Console.WriteLine($"[DebugLog] Adding {synonyms.Count} new Synonyms to the context..."); context.Synonyms.AddRange(synonyms.ToArray()); + Console.WriteLine("[DebugLog] Saving changes..."); context.SaveChanges(); + Console.WriteLine("[DebugLog] Changes saved."); } } } diff --git a/Program.cs b/Program.cs index b25d41a..9755d35 100644 --- a/Program.cs +++ b/Program.cs @@ -5,56 +5,72 @@ using Microsoft.AspNetCore.Identity; var builder = WebApplication.CreateBuilder(args); -var isProduction = (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "") == "Production"; +string aspNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? ""; +Console.WriteLine($"[DebugLog] ASPNETCORE_ENVIRONMENT='{aspNetCoreEnvironment}'"); // Add services to the container. builder.Services.AddRazorPages(); -if (isProduction) +if (aspNetCoreEnvironment == "Production") { + Console.WriteLine("[DebugLog] Production environment detected, using SQL Server connection string..."); builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING") ?? throw new InvalidOperationException("Connection string 'AZURE_SQL_CONNECTIONSTRING' not found."))); + Console.WriteLine("[DebugLog] ISeeGreenContext added, adding StackExchangeRedisCache..."); builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = builder.Configuration["AZURE_REDIS_CONNECTIONSTRING"]; options.InstanceName = "SampleInstance"; }); + Console.WriteLine("[DebugLog] AddStackExchangeRedisCache completed."); } else { + Console.WriteLine("[DebugLog] Non-production environment detected, using SQLite connection string..."); builder.Services.AddDbContext(options => options.UseSqlite(builder.Configuration.GetConnectionString("ISeeGreenContextSQLite") ?? throw new InvalidOperationException("Connection string 'ISeeGreenContextSQLite' not found."))); + Console.WriteLine("[DebugLog] ISeeGreenContext added."); } +Console.WriteLine("[DebugLog] Adding default identity service..."); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = false) .AddEntityFrameworkStores(); +Console.WriteLine("[DebugLog] Adding database developer page exception filter..."); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); +Console.WriteLine("[DebugLog] Building the WebApplication object..."); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { + Console.WriteLine("[DebugLog] Configuring HTTP request pipeline for development environment..."); app.UseExceptionHandler("/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(); } else { + Console.WriteLine("[DebugLog] Configuring HTTP request pipeline for non-development environment..."); app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } +Console.WriteLine("[DebugLog] Creating service scope..."); using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; + Console.WriteLine("[DebugLog] Getting ISeeGreenContext service..."); var context = services.GetRequiredService(); + Console.WriteLine("[DebugLog] Ensuring database has been created..."); context.Database.EnsureCreated(); + Console.WriteLine("[DebugLog] Starting DbInitializer.Initialize(ISeeGreenContext)..."); DbInitializer.Initialize(context); } +Console.WriteLine("[DebugLog] Services have been initialized."); app.UseHttpsRedirection(); app.UseStaticFiles(); @@ -65,4 +81,5 @@ app.MapRazorPages(); +Console.WriteLine("[DebugLog] Running WebApplication..."); app.Run();