Set console title based on DB environment (DEV/PROD)

Added SetConsoleTitleFromCatalog to detect the database environment from the connection string and set the console window title to [PROD] or [DEV] accordingly. Throws on ambiguous catalog names and prints a red warning for production. Switched appsettings.json to use the DEV database. Added Microsoft.Data.SqlClient for connection string parsing. This reduces risk of accidental operations on the wrong database.
This commit is contained in:
Loretta 2026-07-12 08:36:14 +02:00
parent d83ad35f71
commit 59317749bc
1 changed files with 45 additions and 0 deletions

View File

@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -63,6 +64,8 @@ public class PluginNopStartup : INopStartup
// Enforce registration of source generated serializers which might not run due to AssemblyLoadContext restrictions
Mango.Nop.Core.AcBinaryForcedInit.ForceRegister();
SetConsoleTitleFromCatalog(configuration);
services.AddScoped<IAcLogWriterBase, ConsoleLogWriter>();
services.AddTransient<INopLoggerMsSqlNopDataProvider, NopLoggerMsSqlNopDataProvider>();
@ -286,6 +289,48 @@ public class PluginNopStartup : INopStartup
#endif
}
/// <summary>
/// Console title = [PROD]/[DEV] badge from the connection string's Initial Catalog.
/// Both flags require their own positive evidence; an ambiguous catalog name must fail the startup
/// instead of showing a possibly wrong badge.
/// </summary>
private static void SetConsoleTitleFromCatalog(IConfiguration configuration)
{
// Empty during nopCommerce first-run install (no DB configured yet) — no badge, no error.
var connectionString = configuration["ConnectionStrings:ConnectionString"];
if (string.IsNullOrEmpty(connectionString)) return;
var catalog = new SqlConnectionStringBuilder(connectionString).InitialCatalog;
var isDev = catalog.Contains("_DEV", StringComparison.OrdinalIgnoreCase)
|| catalog.Contains("_TEST", StringComparison.OrdinalIgnoreCase);
var isProd = catalog.Contains("_PROD", StringComparison.OrdinalIgnoreCase);
if (isProd == isDev)
throw new InvalidOperationException(
$"Database catalog '{catalog}' cannot be classified: isProd={isProd}, isDev={isDev}. " +
"Fix the catalog naming or the detection rules — refusing to start with an ambiguous environment badge.");
try
{
if (!OperatingSystem.IsWindows()) return;
Console.Title = (isProd ? "[PROD]" : "[DEV]") + " FruitBank server";
if (isProd)
{
var previousColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"PRODUCTION DATABASE: {catalog}");
Console.ForegroundColor = previousColor;
}
}
catch
{
// No console (IIS / Windows service) — badge skipped, classification already ran above.
}
}
/// <summary>
/// Configure the using of added middleware
/// </summary>