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