From 6f138c2c5175038bbf205c9c23ca38a93aced379 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Fri, 23 Jun 2023 00:51:09 +0200 Subject: [PATCH] Improved logging. Added better error handling for mysql database backup --- .../App/Helpers/DatabaseCheckupService.cs | 35 +++++++++++++------ Moonlight/App/Helpers/Logger.cs | 24 ++++++------- Moonlight/App/Services/ConfigService.cs | 4 --- Moonlight/Program.cs | 30 +++++++++++----- 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/Moonlight/App/Helpers/DatabaseCheckupService.cs b/Moonlight/App/Helpers/DatabaseCheckupService.cs index 6fc4cbc1..dc48c424 100644 --- a/Moonlight/App/Helpers/DatabaseCheckupService.cs +++ b/Moonlight/App/Helpers/DatabaseCheckupService.cs @@ -81,19 +81,32 @@ public class DatabaseCheckupService Logger.Info($"Saving it to: {file}"); Logger.Info("Starting backup..."); - var sw = new Stopwatch(); - sw.Start(); + try + { + var sw = new Stopwatch(); + sw.Start(); - await using MySqlConnection conn = new MySqlConnection(connectionString); - await using MySqlCommand cmd = new MySqlCommand(); - using MySqlBackup mb = new MySqlBackup(cmd); + await using MySqlConnection conn = new MySqlConnection(connectionString); + await using MySqlCommand cmd = new MySqlCommand(); + using MySqlBackup mb = new MySqlBackup(cmd); - cmd.Connection = conn; - await conn.OpenAsync(); - mb.ExportToFile(file); - await conn.CloseAsync(); + cmd.Connection = conn; + await conn.OpenAsync(); + mb.ExportToFile(file); + await conn.CloseAsync(); - sw.Stop(); - Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s"); + sw.Stop(); + Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s"); + } + catch (Exception e) + { + Logger.Fatal("-----------------------------------------------"); + Logger.Fatal("Unable to create backup for moonlight database"); + Logger.Fatal("Moonlight will start anyways in 30 seconds"); + Logger.Fatal("-----------------------------------------------"); + Logger.Fatal(e); + + Thread.Sleep(TimeSpan.FromSeconds(30)); + } } } \ No newline at end of file diff --git a/Moonlight/App/Helpers/Logger.cs b/Moonlight/App/Helpers/Logger.cs index 741ef984..8221e082 100644 --- a/Moonlight/App/Helpers/Logger.cs +++ b/Moonlight/App/Helpers/Logger.cs @@ -10,37 +10,37 @@ public static class Logger public static void Verbose(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Verbose("{Message} {Channel}", message, channel); + .Verbose("{Message}", message); } public static void Info(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Information("{Message} {Channel}", message, channel); + .Information("{Message}", message); } public static void Debug(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Debug("{Message} {Channel}", message, channel); + .Debug("{Message}", message); } public static void Error(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Error("{Message} {Channel}", message, channel); + .Error("{Message}", message); } public static void Warn(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Warning("{Message} {Channel}", message, channel); + .Warning("{Message}", message); } public static void Fatal(string message, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Fatal("{Message} {Channel}", message, channel); + .Fatal("{Message}", message); } #endregion @@ -48,37 +48,37 @@ public static class Logger public static void Verbose(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Verbose(exception, "{Channel}", channel); + .Verbose(exception, ""); } public static void Info(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Information(exception, "{Channel}", channel); + .Information(exception, ""); } public static void Debug(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Debug(exception, "{Channel}", channel); + .Debug(exception, ""); } public static void Error(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Error(exception, "{Channel}", channel); + .Error(exception, ""); } public static void Warn(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Warning(exception, "{Channel}", channel); + .Warning(exception, ""); } public static void Fatal(Exception exception, string channel = "default") { Log.ForContext("SourceContext", GetNameOfCallingClass()) - .Fatal(exception, "{Channel}", channel); + .Fatal(exception, ""); } #endregion diff --git a/Moonlight/App/Services/ConfigService.cs b/Moonlight/App/Services/ConfigService.cs index b05ce60c..1e4fbcb8 100644 --- a/Moonlight/App/Services/ConfigService.cs +++ b/Moonlight/App/Services/ConfigService.cs @@ -41,8 +41,6 @@ public class ConfigService : IConfiguration public void Reload() { - Logger.Info($"Reading config from '{PathBuilder.File("storage", "configs", "config.json")}'"); - Configuration = new ConfigurationBuilder().AddJsonStream( new MemoryStream(Encoding.ASCII.GetBytes( File.ReadAllText( @@ -50,8 +48,6 @@ public class ConfigService : IConfiguration ) ) )).Build(); - - Logger.Info("Reloaded configuration file"); } public IEnumerable GetChildren() diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 8743b96e..697ccdd5 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -38,19 +38,31 @@ namespace Moonlight { public static async Task Main(string[] args) { - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Verbose() - .Enrich.FromLogContext() - .WriteTo.Console( - outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") - .CreateLogger(); + // This will also copy all default config files + var configService = new ConfigService(new StorageService()); + if (configService.DebugMode) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Verbose() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + } + else + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .Enrich.FromLogContext() + .WriteTo.Console( + outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + } + Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}"); Logger.Info("Running pre-init tasks"); - - // This will also copy all default config files - var configService = new ConfigService(new StorageService()); var databaseCheckupService = new DatabaseCheckupService(configService); await databaseCheckupService.Perform();