54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Project_Periodensystem.View
|
|
{
|
|
public static class Logger
|
|
{
|
|
private static readonly string LogDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
private static readonly string LogFile = Path.Combine(LogDirectory, "app_log.txt");
|
|
|
|
public static void Log(string message)
|
|
{
|
|
try
|
|
{
|
|
// Ensure directory exists
|
|
Directory.CreateDirectory(LogDirectory);
|
|
|
|
var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - {message}";
|
|
// Write immediately to console for direct feedback
|
|
Console.WriteLine(logMessage);
|
|
|
|
// Append to file with direct flush
|
|
using (StreamWriter sw = File.AppendText(LogFile))
|
|
{
|
|
sw.WriteLine(logMessage);
|
|
sw.Flush();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Logging failed: {ex.Message}");
|
|
Console.WriteLine($"Attempted to write to: {LogFile}");
|
|
Console.WriteLine($"Original message: {message}");
|
|
}
|
|
}
|
|
|
|
static Logger()
|
|
{
|
|
// Clear log on startup
|
|
try
|
|
{
|
|
if (File.Exists(LogFile))
|
|
{
|
|
File.Delete(LogFile);
|
|
}
|
|
Log("=== Logging started ===");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Failed to initialize log file: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
} |