-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractalLogger.cs
More file actions
78 lines (67 loc) · 2.2 KB
/
Copy pathFractalLogger.cs
File metadata and controls
78 lines (67 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Diagnostics;
using System.IO;
namespace Fractals
{
/// <summary>
/// Système de logging pour l'application
/// </summary>
public class FractalLogger : IDisposable
{
private readonly StreamWriter? _logWriter;
private readonly string _logFilePath;
public FractalLogger()
{
_logFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Fractals",
$"fractals_{DateTime.Now:yyyyMMdd}.log"
);
try
{
var logDir = Path.GetDirectoryName(_logFilePath);
if (logDir != null && !Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
_logWriter = new StreamWriter(_logFilePath, append: true)
{
AutoFlush = true
};
_logWriter.WriteLine($"\n========== Session démarrée : {DateTime.Now:yyyy-MM-dd HH:mm:ss} ==========");
}
catch (Exception ex)
{
Debug.WriteLine($"Erreur lors de l'initialisation du logging : {ex.Message}");
}
}
public void Log(string message, string level = "INFO")
{
var timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
var logMessage = $"[{timestamp}] [{level}] {message}";
Debug.WriteLine(logMessage);
Console.WriteLine(logMessage);
try
{
_logWriter?.WriteLine(logMessage);
}
catch (Exception ex)
{
Debug.WriteLine($"Erreur d'écriture dans le log : {ex.Message}");
}
}
public void LogError(string message, Exception? ex = null)
{
var errorMessage = ex != null ? $"{message} : {ex.Message}" : message;
Log(errorMessage, "ERROR");
if (ex != null)
{
Log($"StackTrace: {ex.StackTrace}", "ERROR");
}
}
public void Dispose()
{
_logWriter?.Dispose();
}
}
}