Improved Wave startup logging

This commit is contained in:
Mia Rose Winter 2024-02-07 12:19:33 +01:00
parent eded7f08d5
commit 452c93fca0
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -6,8 +6,6 @@
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using StackExchange.Redis; using StackExchange.Redis;
using Tomlyn.Extensions.Configuration; using Tomlyn.Extensions.Configuration;
@ -17,6 +15,8 @@
using Wave.Services; using Wave.Services;
using Wave.Utilities; using Wave.Utilities;
var logMessages = new List<string>();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Configuration builder.Configuration
.AddJsonFile("/configuration/config.json", true, false) .AddJsonFile("/configuration/config.json", true, false)
@ -56,7 +56,7 @@
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
}); });
Console.WriteLine("No Redis connection string found."); logMessages.Add("No Redis connection string found.");
} }
#endregion #endregion
@ -122,7 +122,7 @@
builder.Services.AddScoped<IEmailSender<ApplicationUser>, SmtpEmailSender>(); builder.Services.AddScoped<IEmailSender<ApplicationUser>, SmtpEmailSender>();
} else { } else {
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>(); builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
Console.WriteLine("No Email provider configured."); logMessages.Add("No Email provider configured.");
} }
#endregion #endregion
@ -167,18 +167,20 @@
app.UseRequestLocalization(); app.UseRequestLocalization();
foreach (string message in logMessages) {
app.Logger.LogInformation("{message}", message);
}
{ {
using var scope = app.Services.CreateScope(); using var scope = app.Services.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate(); context.Database.Migrate();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>(); var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
if (userManager.GetUsersInRoleAsync("Admin").Result.Any() is false) { if (userManager.GetUsersInRoleAsync("Admin").Result.Any() is false) {
string admin = Guid.NewGuid().ToString("N")[..16]; string admin = Guid.NewGuid().ToString("N")[..16];
Console.WriteLine( app.Logger.LogWarning("There is currently no user in your installation with the admin role, " +
"There is currently no user in your installation with the admin role, " + "go to /Admin and use the following password to self promote your account: {admin}", admin);
"go to /Admin and use the following password to self promote your account: " + admin);
File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "admin.txt"), admin); File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "admin.txt"), admin);
} }
} }