Initial commit - Pingerino network monitoring application
هذا الالتزام موجود في:
59
Services/LoggingService.cs
Normal file
59
Services/LoggingService.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
المرجع في مشكلة جديدة
حظر مستخدم