-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.cs
185 lines (156 loc) · 6.59 KB
/
Log.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Logging.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Logging;
using System.Linq;
// ReSharper disable CheckNamespace
public struct MinLogLevelOption
{
/// <summary>
/// Real Class Name
/// </summary>
public string ClassName { get; set; }
/// <summary>
/// Real Method Name
/// </summary>
public string MethodName { get; set; }
public LogLevel MinLogLevel { get; set; }
}
public static class Log
{
private static List<LogVisibility> LogVisibilities { get; set; }
private static List<MinLogLevelOption> MinLogLevels { get; set; }
private static bool _initialized;
private static readonly object ConsoleLock = new object();
public static void Initialize(List<LogVisibility> logVisibilities, List<MinLogLevelOption> minLogLevels)
{
if (_initialized)
{
LogVisibilities.AddRange(logVisibilities);
MinLogLevels.AddRange(minLogLevels);
return;
}
Console.OutputEncoding = Encoding.Unicode;
LogVisibilities = logVisibilities;
MinLogLevels = minLogLevels;
_initialized = true;
LogBasicInformation();
}
private static void LogBasicInformation()
{
if (!LogVisibilities.Contains(LogVisibility.Console)) return;
const string logo = @"
### ######## ###### ######## #### ###### ## ## ####### ## ########
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ######## ## ## ## ## ## ## ## ## ## ## ######
######### ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ###### ## #### ###### ### ### ####### ######## ##
";
Console.WriteLine(logo);
Console.WriteLine("ArcticWolf DataMiner v1.0.0");
Console.WriteLine("© 2021 Joschua Haß (DeveloperJoschi#3193)");
Console.WriteLine();
}
private static void Write(LogLevel logLevel, string message, string prefix = null, string classPrefix = null)
{
var frame = new StackFrame(2);
var method = frame.GetMethod();
var methodDefaultLogPrefix = "";
var classDefaultLogPrefix = method?.DeclaringType?.Name;
if (method != null)
{
foreach (var attr in method.GetCustomAttributes(false))
{
if (attr is LogPrefixAttribute logPrefixAttr)
{
methodDefaultLogPrefix = logPrefixAttr.Prefix;
}
}
if (method.DeclaringType != null)
{
foreach (var attr in method.DeclaringType.GetCustomAttributes(false))
{
if (attr is LogPrefixAttribute logPrefixAttr)
{
classDefaultLogPrefix = logPrefixAttr.Prefix;
}
}
// first check if there is a custom log level setting for the specified function,
// then default to the class setting
if (MinLogLevels.Any(x => x.ClassName == method.DeclaringType.Name &&
x.MethodName == (prefix ?? method.Name)))
{
if (MinLogLevels.First(x => x.ClassName == method.DeclaringType.Name &&
x.MethodName == (prefix ?? method.Name)).MinLogLevel > logLevel)
{
return;
}
}
else if (MinLogLevels.Any(x => x.ClassName == method.DeclaringType.Name &&
string.IsNullOrWhiteSpace(x.MethodName)))
{
if (MinLogLevels.First(x => x.ClassName == method.DeclaringType.Name &&
string.IsNullOrWhiteSpace(x.MethodName)).MinLogLevel > logLevel)
{
return;
}
}
}
}
const string separator = " ";
var formattedDateTime = $"[{DateTime.Now}]";
var formattedLogLevel = $"[{logLevel}]";
var formattedMessage = $"[{classDefaultLogPrefix}] ";
if (classPrefix != null)
{
formattedMessage = classPrefix;
}
if (prefix != null)
{
formattedMessage += $"({prefix}): ";
}
else if (methodDefaultLogPrefix != "")
{
formattedMessage += $"({methodDefaultLogPrefix}): ";
}
formattedMessage += message + Environment.NewLine;
if (!LogVisibilities.Contains(LogVisibility.Console)) return;
lock (ConsoleLock)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(formattedDateTime + separator);
Console.ForegroundColor = logLevel.ToConsoleColor();
Console.Write(formattedLogLevel + separator);
Console.ResetColor();
Console.Write(formattedMessage);
}
}
public static void Verbose(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Verbose, message, customMethodPrefix, customClassPrefix);
}
public static void Debug(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Debug, message, customMethodPrefix, customClassPrefix);
}
public static void Information(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Information, message, customMethodPrefix, customClassPrefix);
}
public static void Warning(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Warning, message, customMethodPrefix, customClassPrefix);
}
public static void Error(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Error, message, customMethodPrefix, customClassPrefix);
}
public static void Fatal(string message, string customMethodPrefix = null, string customClassPrefix = null)
{
Write(LogLevel.Fatal, message, customMethodPrefix, customClassPrefix);
}
}