Skip to content

Commit

Permalink
Added verbose logs to Program.cs and DbInitializer.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
reidst committed Dec 10, 2023
1 parent e2dd7bc commit cb8965d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
42 changes: 40 additions & 2 deletions Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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
Expand All @@ -41,16 +46,22 @@ public static void Initialize(ISeeGreenContext context)
APG4sort = DbCast<long>(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> 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
Expand All @@ -70,16 +81,22 @@ public static void Initialize(ISeeGreenContext context)
SortLevel6 = (int)DbCast<long>(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> 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
Expand All @@ -90,16 +107,22 @@ public static void Initialize(ISeeGreenContext context)
TaxonomicOrderID = DbCast<string?>(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> 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
Expand All @@ -109,16 +132,22 @@ public static void Initialize(ISeeGreenContext context)
FamilyID = DbCast<string?>(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> 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
Expand All @@ -135,16 +164,22 @@ public static void Initialize(ISeeGreenContext context)
USDAsynonym = DbCast<string?>(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> 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
Expand All @@ -158,8 +193,11 @@ public static void Initialize(ISeeGreenContext context)
Authors = DbCast<string?>(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.");
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISeeGreenContext>(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<ISeeGreenContext>(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<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ISeeGreenContext>();

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<ISeeGreenContext>();
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();
Expand All @@ -65,4 +81,5 @@

app.MapRazorPages();

Console.WriteLine("[DebugLog] Running WebApplication...");
app.Run();

0 comments on commit cb8965d

Please sign in to comment.