Init
This commit is contained in:
43
Logger.cs
Normal file
43
Logger.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user