Skip to content

Commit efed2a6

Browse files
Merge pull request #188 from Moonlight-Panel/ImproveLogging
Improved logging. Added better error handling for mysql database backup
2 parents 6c43e6a + 6f138c2 commit efed2a6

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

Moonlight/App/Helpers/DatabaseCheckupService.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,32 @@ public async Task BackupDatabase()
8181
Logger.Info($"Saving it to: {file}");
8282
Logger.Info("Starting backup...");
8383

84-
var sw = new Stopwatch();
85-
sw.Start();
84+
try
85+
{
86+
var sw = new Stopwatch();
87+
sw.Start();
8688

87-
await using MySqlConnection conn = new MySqlConnection(connectionString);
88-
await using MySqlCommand cmd = new MySqlCommand();
89-
using MySqlBackup mb = new MySqlBackup(cmd);
89+
await using MySqlConnection conn = new MySqlConnection(connectionString);
90+
await using MySqlCommand cmd = new MySqlCommand();
91+
using MySqlBackup mb = new MySqlBackup(cmd);
9092

91-
cmd.Connection = conn;
92-
await conn.OpenAsync();
93-
mb.ExportToFile(file);
94-
await conn.CloseAsync();
93+
cmd.Connection = conn;
94+
await conn.OpenAsync();
95+
mb.ExportToFile(file);
96+
await conn.CloseAsync();
9597

96-
sw.Stop();
97-
Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s");
98+
sw.Stop();
99+
Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s");
100+
}
101+
catch (Exception e)
102+
{
103+
Logger.Fatal("-----------------------------------------------");
104+
Logger.Fatal("Unable to create backup for moonlight database");
105+
Logger.Fatal("Moonlight will start anyways in 30 seconds");
106+
Logger.Fatal("-----------------------------------------------");
107+
Logger.Fatal(e);
108+
109+
Thread.Sleep(TimeSpan.FromSeconds(30));
110+
}
98111
}
99112
}

Moonlight/App/Helpers/Logger.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,75 @@ public static class Logger
1010
public static void Verbose(string message, string channel = "default")
1111
{
1212
Log.ForContext("SourceContext", GetNameOfCallingClass())
13-
.Verbose("{Message} {Channel}", message, channel);
13+
.Verbose("{Message}", message);
1414
}
1515

1616
public static void Info(string message, string channel = "default")
1717
{
1818
Log.ForContext("SourceContext", GetNameOfCallingClass())
19-
.Information("{Message} {Channel}", message, channel);
19+
.Information("{Message}", message);
2020
}
2121

2222
public static void Debug(string message, string channel = "default")
2323
{
2424
Log.ForContext("SourceContext", GetNameOfCallingClass())
25-
.Debug("{Message} {Channel}", message, channel);
25+
.Debug("{Message}", message);
2626
}
2727

2828
public static void Error(string message, string channel = "default")
2929
{
3030
Log.ForContext("SourceContext", GetNameOfCallingClass())
31-
.Error("{Message} {Channel}", message, channel);
31+
.Error("{Message}", message);
3232
}
3333

3434
public static void Warn(string message, string channel = "default")
3535
{
3636
Log.ForContext("SourceContext", GetNameOfCallingClass())
37-
.Warning("{Message} {Channel}", message, channel);
37+
.Warning("{Message}", message);
3838
}
3939

4040
public static void Fatal(string message, string channel = "default")
4141
{
4242
Log.ForContext("SourceContext", GetNameOfCallingClass())
43-
.Fatal("{Message} {Channel}", message, channel);
43+
.Fatal("{Message}", message);
4444
}
4545
#endregion
4646

4747
#region Exception method calls
4848
public static void Verbose(Exception exception, string channel = "default")
4949
{
5050
Log.ForContext("SourceContext", GetNameOfCallingClass())
51-
.Verbose(exception, "{Channel}", channel);
51+
.Verbose(exception, "");
5252
}
5353

5454
public static void Info(Exception exception, string channel = "default")
5555
{
5656
Log.ForContext("SourceContext", GetNameOfCallingClass())
57-
.Information(exception, "{Channel}", channel);
57+
.Information(exception, "");
5858
}
5959

6060
public static void Debug(Exception exception, string channel = "default")
6161
{
6262
Log.ForContext("SourceContext", GetNameOfCallingClass())
63-
.Debug(exception, "{Channel}", channel);
63+
.Debug(exception, "");
6464
}
6565

6666
public static void Error(Exception exception, string channel = "default")
6767
{
6868
Log.ForContext("SourceContext", GetNameOfCallingClass())
69-
.Error(exception, "{Channel}", channel);
69+
.Error(exception, "");
7070
}
7171

7272
public static void Warn(Exception exception, string channel = "default")
7373
{
7474
Log.ForContext("SourceContext", GetNameOfCallingClass())
75-
.Warning(exception, "{Channel}", channel);
75+
.Warning(exception, "");
7676
}
7777

7878
public static void Fatal(Exception exception, string channel = "default")
7979
{
8080
Log.ForContext("SourceContext", GetNameOfCallingClass())
81-
.Fatal(exception, "{Channel}", channel);
81+
.Fatal(exception, "");
8282
}
8383
#endregion
8484

Moonlight/App/Services/ConfigService.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ public ConfigService(StorageService storageService)
4141

4242
public void Reload()
4343
{
44-
Logger.Info($"Reading config from '{PathBuilder.File("storage", "configs", "config.json")}'");
45-
4644
Configuration = new ConfigurationBuilder().AddJsonStream(
4745
new MemoryStream(Encoding.ASCII.GetBytes(
4846
File.ReadAllText(
4947
PathBuilder.File("storage", "configs", "config.json")
5048
)
5149
)
5250
)).Build();
53-
54-
Logger.Info("Reloaded configuration file");
5551
}
5652

5753
public IEnumerable<IConfigurationSection> GetChildren()

Moonlight/Program.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,31 @@ public class Program
3838
{
3939
public static async Task Main(string[] args)
4040
{
41-
Log.Logger = new LoggerConfiguration()
42-
.MinimumLevel.Verbose()
43-
.Enrich.FromLogContext()
44-
.WriteTo.Console(
45-
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
46-
.CreateLogger();
41+
// This will also copy all default config files
42+
var configService = new ConfigService(new StorageService());
4743

44+
if (configService.DebugMode)
45+
{
46+
Log.Logger = new LoggerConfiguration()
47+
.MinimumLevel.Verbose()
48+
.Enrich.FromLogContext()
49+
.WriteTo.Console(
50+
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
51+
.CreateLogger();
52+
}
53+
else
54+
{
55+
Log.Logger = new LoggerConfiguration()
56+
.MinimumLevel.Information()
57+
.Enrich.FromLogContext()
58+
.WriteTo.Console(
59+
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
60+
.CreateLogger();
61+
}
62+
4863
Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}");
4964

5065
Logger.Info("Running pre-init tasks");
51-
52-
// This will also copy all default config files
53-
var configService = new ConfigService(new StorageService());
5466
var databaseCheckupService = new DatabaseCheckupService(configService);
5567

5668
await databaseCheckupService.Perform();

0 commit comments

Comments
 (0)