Merge pull request #188 from Moonlight-Panel/ImproveLogging

Improved logging. Added better error handling for mysql database backup
This commit is contained in:
Marcel Baumgartner
2023-06-23 00:51:32 +02:00
committed by GitHub
4 changed files with 57 additions and 36 deletions

View File

@@ -81,19 +81,32 @@ public class DatabaseCheckupService
Logger.Info($"Saving it to: {file}"); Logger.Info($"Saving it to: {file}");
Logger.Info("Starting backup..."); Logger.Info("Starting backup...");
var sw = new Stopwatch(); try
sw.Start(); {
var sw = new Stopwatch();
sw.Start();
await using MySqlConnection conn = new MySqlConnection(connectionString); await using MySqlConnection conn = new MySqlConnection(connectionString);
await using MySqlCommand cmd = new MySqlCommand(); await using MySqlCommand cmd = new MySqlCommand();
using MySqlBackup mb = new MySqlBackup(cmd); using MySqlBackup mb = new MySqlBackup(cmd);
cmd.Connection = conn; cmd.Connection = conn;
await conn.OpenAsync(); await conn.OpenAsync();
mb.ExportToFile(file); mb.ExportToFile(file);
await conn.CloseAsync(); await conn.CloseAsync();
sw.Stop(); sw.Stop();
Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s"); 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));
}
} }
} }

View File

@@ -10,37 +10,37 @@ public static class Logger
public static void Verbose(string message, string channel = "default") public static void Verbose(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Verbose("{Message} {Channel}", message, channel); .Verbose("{Message}", message);
} }
public static void Info(string message, string channel = "default") public static void Info(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Information("{Message} {Channel}", message, channel); .Information("{Message}", message);
} }
public static void Debug(string message, string channel = "default") public static void Debug(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Debug("{Message} {Channel}", message, channel); .Debug("{Message}", message);
} }
public static void Error(string message, string channel = "default") public static void Error(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Error("{Message} {Channel}", message, channel); .Error("{Message}", message);
} }
public static void Warn(string message, string channel = "default") public static void Warn(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Warning("{Message} {Channel}", message, channel); .Warning("{Message}", message);
} }
public static void Fatal(string message, string channel = "default") public static void Fatal(string message, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Fatal("{Message} {Channel}", message, channel); .Fatal("{Message}", message);
} }
#endregion #endregion
@@ -48,37 +48,37 @@ public static class Logger
public static void Verbose(Exception exception, string channel = "default") public static void Verbose(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Verbose(exception, "{Channel}", channel); .Verbose(exception, "");
} }
public static void Info(Exception exception, string channel = "default") public static void Info(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Information(exception, "{Channel}", channel); .Information(exception, "");
} }
public static void Debug(Exception exception, string channel = "default") public static void Debug(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Debug(exception, "{Channel}", channel); .Debug(exception, "");
} }
public static void Error(Exception exception, string channel = "default") public static void Error(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Error(exception, "{Channel}", channel); .Error(exception, "");
} }
public static void Warn(Exception exception, string channel = "default") public static void Warn(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Warning(exception, "{Channel}", channel); .Warning(exception, "");
} }
public static void Fatal(Exception exception, string channel = "default") public static void Fatal(Exception exception, string channel = "default")
{ {
Log.ForContext("SourceContext", GetNameOfCallingClass()) Log.ForContext("SourceContext", GetNameOfCallingClass())
.Fatal(exception, "{Channel}", channel); .Fatal(exception, "");
} }
#endregion #endregion

View File

@@ -41,8 +41,6 @@ public class ConfigService : IConfiguration
public void Reload() public void Reload()
{ {
Logger.Info($"Reading config from '{PathBuilder.File("storage", "configs", "config.json")}'");
Configuration = new ConfigurationBuilder().AddJsonStream( Configuration = new ConfigurationBuilder().AddJsonStream(
new MemoryStream(Encoding.ASCII.GetBytes( new MemoryStream(Encoding.ASCII.GetBytes(
File.ReadAllText( File.ReadAllText(
@@ -50,8 +48,6 @@ public class ConfigService : IConfiguration
) )
) )
)).Build(); )).Build();
Logger.Info("Reloaded configuration file");
} }
public IEnumerable<IConfigurationSection> GetChildren() public IEnumerable<IConfigurationSection> GetChildren()

View File

@@ -38,19 +38,31 @@ namespace Moonlight
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
Log.Logger = new LoggerConfiguration() // This will also copy all default config files
.MinimumLevel.Verbose() var configService = new ConfigService(new StorageService());
.Enrich.FromLogContext()
.WriteTo.Console( if (configService.DebugMode)
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") {
.CreateLogger(); 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($"Working dir: {Directory.GetCurrentDirectory()}");
Logger.Info("Running pre-init tasks"); 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); var databaseCheckupService = new DatabaseCheckupService(configService);
await databaseCheckupService.Perform(); await databaseCheckupService.Perform();