-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Logger.cs
118 lines (105 loc) · 3.68 KB
/
Logger.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace Fenrus;
/// <summary>
/// Logger for Fenrus
/// </summary>
public class Logger
{
static NLog.Logger NLogger;
/// <summary>
/// Logs an information message
/// </summary>
/// <param name="message">the message to log</param>
public static void ILog(string message)
=> Log(LogLevel.Information, message);
/// <summary>
/// Logs an debug message
/// </summary>
/// <param name="message">the message to log</param>
public static void DLog(string message)
=> Log(LogLevel.Debug, message);
/// <summary>
/// Logs an warning message
/// </summary>
/// <param name="message">the message to log</param>
public static void WLog(string message)
=> Log(LogLevel.Warning, message);
/// <summary>
/// Logs an error message
/// </summary>
/// <param name="message">the message to log</param>
public static void ELog(string message)
=> Log(LogLevel.Error, message);
/// <summary>
/// Writes a log message
/// </summary>
/// <param name="level">the level to write</param>
/// <param name="message">the message to write</param>
private static void Log(LogLevel level, string message)
{
// LogCustomConsole(level, message);
switch (level)
{
case LogLevel.Debug:
NLogger.Debug(message);
break;
case LogLevel.Information:
NLogger.Info(message);
break;
case LogLevel.Warning:
NLogger.Warn(message);
break;
case LogLevel.Error:
NLogger.Error(message);
break;
case LogLevel.Trace:
NLogger.Trace(message);
break;
case LogLevel.Critical:
NLogger.Fatal(message);
break;
}
}
/// <summary>
/// Constructs a custom message and writes it directly to the console
/// Use if not using NLogger
/// </summary>
/// <param name="level">the message level</param>
/// <param name="message">the message text</param>
private static void LogCustomConsole(LogLevel level, string message)
{
string lvl = level switch
{
LogLevel.Information => "INFO",
LogLevel.Warning => "WARN",
LogLevel.Error => "ERR",
LogLevel.Trace => "TRCE",
_ => "DBUG"
};
string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " [" + lvl + "] -> " + message;
Console.WriteLine(msg);
}
/// <summary>
/// Initializes the logger
/// </summary>
public static void Initialize()
{
var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to: File and Console
var logfile = new NLog.Targets.FileTarget("logfile")
{
FileName = Path.Combine(DirectoryHelper.GetLogsDirectory(), "Fenrus_${shortdate}.log"),
MaxArchiveDays = 5,
Layout = "${longdate} [${uppercase:${level:format=TriLetter}}] => ${message}"
};
var logconsole = new NLog.Targets.ConsoleTarget("logconsole")
{
Layout = "${longdate} [${uppercase:${level:format=TriLetter}}] => ${message}"
};
// Rules for mapping loggers to targets
config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, logconsole);
config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);
// Apply config
NLog.LogManager.Configuration = config;
NLogger = NLog.LogManager.LogFactory.GetLogger(string.Empty);
}
}