60 أسطر
1.8 KiB
C#
60 أسطر
1.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using Pingerino.Services.Interfaces;
|
|
|
|
namespace Pingerino.Services
|
|
{
|
|
public class LoggingService : ILoggingService
|
|
{
|
|
private readonly string _logFilePath;
|
|
private readonly object _lockObject = new object();
|
|
|
|
public LoggingService()
|
|
{
|
|
var logDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Pingerino", "Logs");
|
|
Directory.CreateDirectory(logDirectory);
|
|
_logFilePath = Path.Combine(logDirectory, $"pingerino_{DateTime.Now:yyyyMMdd}.log");
|
|
}
|
|
|
|
public void LogInformation(string message)
|
|
{
|
|
WriteLog(LogLevel.Information, message);
|
|
}
|
|
|
|
public void LogWarning(string message)
|
|
{
|
|
WriteLog(LogLevel.Warning, message);
|
|
}
|
|
|
|
public void LogError(string message, Exception exception = null)
|
|
{
|
|
var fullMessage = exception != null ? $"{message} - Exception: {exception}" : message;
|
|
WriteLog(LogLevel.Error, fullMessage);
|
|
}
|
|
|
|
public void LogDebug(string message)
|
|
{
|
|
WriteLog(LogLevel.Debug, message);
|
|
}
|
|
|
|
private void WriteLog(LogLevel level, string message)
|
|
{
|
|
try
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
var logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{level}] {message}";
|
|
File.AppendAllText(_logFilePath, logEntry + Environment.NewLine);
|
|
|
|
// Also write to console for debugging
|
|
Console.WriteLine(logEntry);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Ignore logging errors to prevent infinite loops
|
|
}
|
|
}
|
|
}
|
|
}
|