Skip to content

Commit b167720

Browse files
committed
Log to file if possible
1 parent ac0c3ef commit b167720

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,7 @@ MigrationBackup/
360360
.ionide/
361361

362362
# Fody - auto-generated XML schema
363-
FodyWeavers.xsd
363+
FodyWeavers.xsd
364+
365+
### Project-specific
366+
Builds/**

SMSMService/Log.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public static class Log
44
{
55
private static string ServerName = "Minecraft Server";
6-
6+
public static StreamWriter? LogFile;
77
public static void Server(string message)
88
{
99
string FormattedMsg = $"[SRV][{ServerName}] {message}";
@@ -14,18 +14,21 @@ public static void Info(string message)
1414
{
1515
string FormattedMsg = $"[INF][{ServerName}] {message}";
1616
Console.WriteLine(FormattedMsg);
17+
if (LogFile != null) { LogFile.WriteLine(FormattedMsg); }
1718
Handlers?.Invoke(FormattedMsg);
1819
}
1920
public static void Warn(string message)
2021
{
2122
string FormattedMsg = $"[WRN][{ServerName}] {message}";
2223
Console.WriteLine(FormattedMsg);
24+
if (LogFile != null) { LogFile.WriteLine(FormattedMsg); }
2325
Handlers?.Invoke(FormattedMsg);
2426
}
2527
public static void Error(string message)
2628
{
2729
string FormattedMsg = $"[ERR][{ServerName}] {message}";
2830
Console.WriteLine(FormattedMsg);
31+
if (LogFile != null) { LogFile.WriteLine(FormattedMsg); }
2932
Handlers?.Invoke(FormattedMsg);
3033
}
3134

SMSMService/SMSM.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class SMSM : BackgroundService
1515
private readonly ILogger<SMSM> Logger;
1616
private readonly IHostApplicationLifetime Lifetime;
1717

18-
public SMSM(ILogger<SMSM> logger, IHostApplicationLifetime lifetime)
18+
public SMSM(ILogger<SMSM> logger, IHostApplicationLifetime lifetime) // TODO: Get the logger to output to the Windows event log, and redirect output there.
1919
{
20+
Log.Info("SMSM initializing");
2021
this.Logger = logger;
2122
this.Lifetime = lifetime;
2223
this.Lifetime.ApplicationStopping.Register(Stop);

SMSMService/Start.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ public static async Task Main(string[] args)
66
{
77
if (args.Length != 1) { Console.WriteLine("Please specify the config file path as command-line argument."); Environment.Exit(-3); }
88
SMSM.ConfigFile = args[0];
9+
10+
string LogDate = DateTime.Now.ToString("yyyy-MM-dd\\_HH-mm-ss");
11+
try { Log.LogFile = new StreamWriter(File.Create($"C:\\Games\\Minecraft\\LTS\\SMSMLog-{LogDate}.txt")) { AutoFlush = true }; }
12+
catch (Exception Exc)
13+
{
14+
Log.Error("Could not create log file.");
15+
Log.Error(Exc.ToString());
16+
}
17+
918
//Console.TreatControlCAsInput = true;
1019

1120
IHost host = Host.CreateDefaultBuilder(args)

0 commit comments

Comments
 (0)