Improved logging. Added better error handling for mysql database backup

This commit is contained in:
Marcel Baumgartner
2023-06-23 00:51:09 +02:00
parent 6c43e6a533
commit 6f138c2c51
4 changed files with 57 additions and 36 deletions

View File

@@ -81,6 +81,8 @@ public class DatabaseCheckupService
Logger.Info($"Saving it to: {file}"); Logger.Info($"Saving it to: {file}");
Logger.Info("Starting backup..."); Logger.Info("Starting backup...");
try
{
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
@@ -96,4 +98,15 @@ public class DatabaseCheckupService
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

@@ -37,6 +37,11 @@ namespace Moonlight
public class Program public class Program
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{
// This will also copy all default config files
var configService = new ConfigService(new StorageService());
if (configService.DebugMode)
{ {
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose() .MinimumLevel.Verbose()
@@ -44,13 +49,20 @@ namespace Moonlight
.WriteTo.Console( .WriteTo.Console(
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}") outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
.CreateLogger(); .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();