Added detailed logging to startup

This commit is contained in:
Pat Hartl 2023-08-11 15:12:16 -05:00
parent a7707a84c7
commit a146bd9810

View file

@ -9,14 +9,20 @@ using Microsoft.IdentityModel.Tokens;
using NLog.Web; using NLog.Web;
using System.Text; using System.Text;
using Hangfire; using Hangfire;
using NLog;
Logger Logger = LogManager.GetCurrentClassLogger();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration; ConfigurationManager configuration = builder.Configuration;
// Add services to the container. // Add services to the container.
Logger.Debug("Loading settings");
var settings = SettingService.GetSettings(); var settings = SettingService.GetSettings();
Logger.Debug("Loaded!");
Logger.Debug("Configuring MVC and Blazor");
builder.Services.AddMvc(options => options.EnableEndpointRouting = false); builder.Services.AddMvc(options => options.EnableEndpointRouting = false);
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor().AddCircuitOptions(option => builder.Services.AddServerSideBlazor().AddCircuitOptions(option =>
@ -28,12 +34,14 @@ builder.Services.AddServerSideBlazor().AddCircuitOptions(option =>
option.DisableImplicitFromServicesParameters = true; option.DisableImplicitFromServicesParameters = true;
}); });
Logger.Debug("Starting web server on port {Port}", settings.Port);
builder.WebHost.ConfigureKestrel(options => builder.WebHost.ConfigureKestrel(options =>
{ {
// Configure as HTTP only // Configure as HTTP only
options.ListenAnyIP(settings.Port); options.ListenAnyIP(settings.Port);
}); });
Logger.Debug("Initializing DatabaseContext with connection string {ConnectionString}", settings.DatabaseConnectionString);
builder.Services.AddDbContext<LANCommander.Data.DatabaseContext>(b => builder.Services.AddDbContext<LANCommander.Data.DatabaseContext>(b =>
{ {
b.UseLazyLoadingProxies(); b.UseLazyLoadingProxies();
@ -42,6 +50,7 @@ builder.Services.AddDbContext<LANCommander.Data.DatabaseContext>(b =>
builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDatabaseDeveloperPageExceptionFilter();
Logger.Debug("Initializing Identity")
builder.Services.AddDefaultIdentity<User>((IdentityOptions options) => builder.Services.AddDefaultIdentity<User>((IdentityOptions options) =>
{ {
options.SignIn.RequireConfirmedAccount = false; options.SignIn.RequireConfirmedAccount = false;
@ -77,11 +86,13 @@ builder.Services.AddAuthentication(options =>
}; };
}); });
Logger.Debug("Initializing Controllers");
builder.Services.AddControllers().AddJsonOptions(x => builder.Services.AddControllers().AddJsonOptions(x =>
{ {
x.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles; x.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
}); });
Logger.Debug("Initializing Hangfire");
builder.Services.AddHangfire(configuration => builder.Services.AddHangfire(configuration =>
configuration configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
@ -90,13 +101,16 @@ builder.Services.AddHangfire(configuration =>
.UseInMemoryStorage()); .UseInMemoryStorage());
builder.Services.AddHangfireServer(); builder.Services.AddHangfireServer();
Logger.Debug("Registering Swashbuckle");
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
Logger.Debug("Registering AntDesign Blazor");
builder.Services.AddAntDesign(); builder.Services.AddAntDesign();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
Logger.Debug("Registering Services");
builder.Services.AddScoped<SettingService>(); builder.Services.AddScoped<SettingService>();
builder.Services.AddScoped<ArchiveService>(); builder.Services.AddScoped<ArchiveService>();
builder.Services.AddScoped<CategoryService>(); builder.Services.AddScoped<CategoryService>();
@ -113,7 +127,10 @@ builder.Services.AddScoped<GameSaveService>();
builder.Services.AddSingleton<ServerProcessService>(); builder.Services.AddSingleton<ServerProcessService>();
if (settings.Beacon) if (settings.Beacon)
{
Logger.Debug("The beacons have been lit! LANCommander calls for players!");
builder.Services.AddHostedService<BeaconService>(); builder.Services.AddHostedService<BeaconService>();
}
builder.WebHost.UseStaticWebAssets(); builder.WebHost.UseStaticWebAssets();
@ -124,11 +141,13 @@ builder.WebHost.UseKestrel(options =>
builder.Host.UseNLog(); builder.Host.UseNLog();
Logger.Debug("Building Application");
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
Logger.Debug("App has been run in a development environment");
app.UseMigrationsEndPoint(); app.UseMigrationsEndPoint();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
@ -154,6 +173,7 @@ app.UseMvcWithDefaultRoute();
app.MapHub<GameServerHub>("/hubs/gameserver"); app.MapHub<GameServerHub>("/hubs/gameserver");
Logger.Debug("Registering Endpoints");
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapBlazorHub(); endpoints.MapBlazorHub();
@ -161,6 +181,7 @@ app.UseEndpoints(endpoints =>
endpoints.MapControllers(); endpoints.MapControllers();
}); });
Logger.Debug("Ensuring required directories exist");
if (!Directory.Exists("Upload")) if (!Directory.Exists("Upload"))
Directory.CreateDirectory("Upload"); Directory.CreateDirectory("Upload");
@ -174,11 +195,13 @@ if (!Directory.Exists("Snippets"))
Directory.CreateDirectory("Snippets"); Directory.CreateDirectory("Snippets");
// Migrate // Migrate
Logger.Debug("Migrating database if required");
await using var scope = app.Services.CreateAsyncScope(); await using var scope = app.Services.CreateAsyncScope();
using var db = scope.ServiceProvider.GetService<DatabaseContext>(); using var db = scope.ServiceProvider.GetService<DatabaseContext>();
await db.Database.MigrateAsync(); await db.Database.MigrateAsync();
// Autostart any server processes // Autostart any server processes
Logger.Debug("Autostarting Servers");
var serverService = scope.ServiceProvider.GetService<ServerService>(); var serverService = scope.ServiceProvider.GetService<ServerService>();
var serverProcessService = scope.ServiceProvider.GetService<ServerProcessService>(); var serverProcessService = scope.ServiceProvider.GetService<ServerProcessService>();
@ -186,6 +209,8 @@ foreach (var server in await serverService.Get(s => s.Autostart).ToListAsync())
{ {
try try
{ {
Logger.Debug("Autostarting server {ServerName} with a delay of {AutostartDelay} seconds", server.Name, server.AutostartDelay);
if (server.AutostartDelay > 0) if (server.AutostartDelay > 0)
await Task.Delay(server.AutostartDelay); await Task.Delay(server.AutostartDelay);
@ -193,7 +218,7 @@ foreach (var server in await serverService.Get(s => s.Autostart).ToListAsync())
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Debug(ex, "An unexpected error occurred while trying to autostart the server {ServerName}", server.Name);
} }
} }