63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace QoSManager.Services
|
|
{
|
|
public class LoggingService : IDisposable
|
|
{
|
|
private readonly string _logPath;
|
|
|
|
public LoggingService()
|
|
{
|
|
// Get directory of the running executable (works for both regular and single-file apps)
|
|
var exeDir = AppContext.BaseDirectory;
|
|
_logPath = Path.Combine(exeDir, "QoSManager.log");
|
|
|
|
// Create directory if it doesn't exist (should already exist since exe is there)
|
|
var logDir = Path.GetDirectoryName(_logPath);
|
|
if (!Directory.Exists(logDir))
|
|
{
|
|
Directory.CreateDirectory(logDir);
|
|
}
|
|
}
|
|
|
|
public void LogInformation(string message, bool enabled = true)
|
|
{
|
|
if (!enabled) return;
|
|
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
var logMessage = $"[{timestamp}] [INFO] {message}";
|
|
File.AppendAllText(_logPath, logMessage + Environment.NewLine);
|
|
}
|
|
|
|
public void LogError(string message, bool enabled = true)
|
|
{
|
|
if (!enabled) return;
|
|
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
var logMessage = $"[{timestamp}] [ERROR] {message}";
|
|
File.AppendAllText(_logPath, logMessage + Environment.NewLine);
|
|
}
|
|
|
|
public void LogError(Exception exception, string message, bool enabled = true)
|
|
{
|
|
if (!enabled) return;
|
|
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
var logMessage = $"[{timestamp}] [ERROR] {message}: {exception.Message}";
|
|
File.AppendAllText(_logPath, logMessage + Environment.NewLine);
|
|
}
|
|
|
|
public string GetLogFilePath()
|
|
{
|
|
return _logPath;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Ресурсы освобождаются автоматически
|
|
}
|
|
}
|
|
}
|