This commit is contained in:
Nekura
2025-07-25 18:39:48 +02:00
commit b9d7b1c9f7
14 changed files with 1335 additions and 0 deletions

43
Logger.cs Normal file
View File

@@ -0,0 +1,43 @@
// Logger.cs
using System;
using System.IO;
namespace FreelancerListServer
{
public class Logger
{
private readonly string _logDirectory;
private string _currentLogFile;
private DateTime _nextRotation;
private readonly object _lock = new object();
public Logger(string logDirectory = "logs")
{
_logDirectory = logDirectory;
Directory.CreateDirectory(_logDirectory);
UpdateLogFileName();
}
private void UpdateLogFileName()
{
var now = DateTime.Now;
_currentLogFile = Path.Combine(_logDirectory, $"server_{now:yyyyMMdd_HH}.log");
_nextRotation = now.AddHours(1).Date.AddHours(now.Hour + 1);
}
public void Log(string message)
{
lock (_lock)
{
if (DateTime.Now >= _nextRotation)
{
UpdateLogFileName();
}
string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}";
Console.WriteLine(logEntry);
File.AppendAllText(_currentLogFile, logEntry + Environment.NewLine);
}
}
}
}