Repository navigation
Expand file tree
/
Copy pathLogger.cs
More file actions
94 lines (81 loc) · 2.72 KB
/
Copy pathLogger.cs
File metadata and controls
94 lines (81 loc) · 2.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.IO;
using System.Text;
namespace EmailHeaderAnalyzer
{
public static class Logger
{
private static readonly string LogDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"EmailHeaderAnalyzer",
"Logs"
);
private static readonly string LogFilePath = Path.Combine(
LogDirectory,
$"EmailHeaderAnalyzer_{DateTime.Now:yyyyMMdd}.log"
);
private static readonly object _lock = new();
static Logger()
{
try
{
// Create log directory if it doesn't exist
Directory.CreateDirectory(LogDirectory);
}
catch
{
// If we can't create the log directory, logging will be disabled
}
}
public static string GetLogPath()
{
return LogFilePath;
}
public static void LogInfo(string message)
{
WriteLog("INFO", message);
}
public static void LogError(string message, Exception? ex = null)
{
var errorMessage = new StringBuilder();
errorMessage.AppendLine(message);
if (ex != null)
{
errorMessage.AppendLine($"Exception Type: {ex.GetType().FullName}");
errorMessage.AppendLine($"Message: {ex.Message}");
errorMessage.AppendLine($"Stack Trace: {ex.StackTrace}");
if (ex.InnerException != null)
{
errorMessage.AppendLine("Inner Exception:");
errorMessage.AppendLine($" Type: {ex.InnerException.GetType().FullName}");
errorMessage.AppendLine($" Message: {ex.InnerException.Message}");
errorMessage.AppendLine($" Stack Trace: {ex.InnerException.StackTrace}");
}
}
WriteLog("ERROR", errorMessage.ToString());
}
public static void LogDebug(string message)
{
WriteLog("DEBUG", message);
}
public static void LogWarn(string message)
{
WriteLog("WARN", message);
}
private static void WriteLog(string level, string message)
{
try
{
var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level}] {message}{Environment.NewLine}";
lock (_lock)
{
File.AppendAllText(LogFilePath, logEntry);
}
}
catch
{
// Silently fail if we can't write to the log file
}
}
}
}