add proper logger, saves to file as well

This commit is contained in:
Master
2023-10-14 19:39:26 +02:00
parent a2e27effff
commit 239c4fca32
17 changed files with 402 additions and 140 deletions

View File

@@ -1,12 +1,12 @@
using System.Net.Sockets;
using System.Net;
using AscNet.Common.Util;
using AscNet.Logging;
namespace AscNet.GameServer
{
public class Server
{
public static readonly Logger c = new(nameof(GameServer), ConsoleColor.Cyan);
public static Logger log;
public readonly Dictionary<string, Session> Sessions = new();
private static Server? _instance;
private readonly TcpListener listener;
@@ -21,6 +21,10 @@ namespace AscNet.GameServer
public Server()
{
// TODO: add loglevel based on appsettings
LogLevel logLevel = LogLevel.DEBUG;
LogLevel fileLogLevel = LogLevel.DEBUG;
log = new(typeof(Server), logLevel, fileLogLevel);
listener = new(IPAddress.Parse("0.0.0.0"), Common.Common.config.GameServer.Port);
}
@@ -31,21 +35,21 @@ namespace AscNet.GameServer
try
{
listener.Start();
c.Log($"{nameof(GameServer)} started and listening on port {Common.Common.config.GameServer.Port}");
log.Info($"{nameof(GameServer)} started and listening on port {Common.Common.config.GameServer.Port}");
while (true)
{
TcpClient tcpClient = listener.AcceptTcpClient();
string id = tcpClient.Client.RemoteEndPoint!.ToString()!;
c.Warn($"{id} connected");
log.Warn($"{id} connected");
Sessions.Add(id, new Session(id, tcpClient));
}
}
catch (Exception ex)
{
c.Error("TCP listener error: " + ex.Message);
c.Log("Waiting 3 seconds before restarting...");
log.Error("TCP listener error: " + ex.Message);
log.Info("Waiting 3 seconds before restarting...");
Thread.Sleep(3000);
}
}