136 lines
4.1 KiB
C#
136 lines
4.1 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Project_Periodensystem.Model
|
|
{
|
|
/// <summary>
|
|
/// Einfacher Logger für Debug-Ausgaben und Fehlerprotokollierung
|
|
/// </summary>
|
|
public static class Logger
|
|
{
|
|
private static readonly string LogFilePath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"Periodensystem", "app.log");
|
|
|
|
/// <summary>
|
|
/// Statischer Initializer - erstellt Log-Verzeichnis
|
|
/// </summary>
|
|
static Logger()
|
|
{
|
|
try
|
|
{
|
|
var logDirectory = Path.GetDirectoryName(LogFilePath);
|
|
if (logDirectory != null && !Directory.Exists(logDirectory))
|
|
{
|
|
Directory.CreateDirectory(logDirectory);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Falls Log-Datei nicht erstellt werden kann, nur Konsole verwenden
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Protokolliert eine Nachricht sowohl in Konsole als auch Datei
|
|
/// </summary>
|
|
/// <param name="message">Zu protokollierende Nachricht</param>
|
|
public static void Log(string message)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
return;
|
|
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
var logEntry = $"[{timestamp}] {message}";
|
|
|
|
// Konsolen-Ausgabe (für Debug)
|
|
Console.WriteLine(logEntry);
|
|
|
|
// Datei-Ausgabe (für Persistenz)
|
|
try
|
|
{
|
|
File.AppendAllText(LogFilePath, logEntry + Environment.NewLine);
|
|
}
|
|
catch
|
|
{
|
|
// Fehler beim Schreiben ignorieren um App nicht zum Absturz zu bringen
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Protokolliert eine Exception mit Stack-Trace
|
|
/// </summary>
|
|
/// <param name="ex">Exception die protokolliert werden soll</param>
|
|
/// <param name="context">Zusätzlicher Kontext</param>
|
|
public static void LogException(Exception ex, string context = "")
|
|
{
|
|
if (ex == null)
|
|
{
|
|
Log("Null-Exception übergeben");
|
|
return;
|
|
}
|
|
|
|
var message = string.IsNullOrWhiteSpace(context)
|
|
? $"EXCEPTION: {ex.Message}\nStack: {ex.StackTrace}"
|
|
: $"EXCEPTION in {context}: {ex.Message}\nStack: {ex.StackTrace}";
|
|
|
|
Log(message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Protokolliert eine Warnung
|
|
/// </summary>
|
|
/// <param name="message">Warnung</param>
|
|
public static void LogWarning(string message)
|
|
{
|
|
Log($"WARNING: {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Protokolliert einen Fehler
|
|
/// </summary>
|
|
/// <param name="message">Fehlermeldung</param>
|
|
public static void LogError(string message)
|
|
{
|
|
Log($"ERROR: {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Protokolliert Debug-Informationen (nur in Debug-Build)
|
|
/// </summary>
|
|
/// <param name="message">Debug-Nachricht</param>
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
|
public static void LogDebug(string message)
|
|
{
|
|
Log($"DEBUG: {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löscht die Log-Datei (für Cleanup)
|
|
/// </summary>
|
|
public static void ClearLog()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(LogFilePath))
|
|
{
|
|
File.Delete(LogFilePath);
|
|
Log("Log-Datei gelöscht");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log($"Fehler beim Löschen der Log-Datei: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt den Pfad zur Log-Datei zurück
|
|
/// </summary>
|
|
/// <returns>Vollständiger Pfad zur Log-Datei</returns>
|
|
public static string GetLogFilePath()
|
|
{
|
|
return LogFilePath;
|
|
}
|
|
}
|
|
} |