-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoggerBase.cs
287 lines (257 loc) · 10.5 KB
/
LoggerBase.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
namespace DotStd
{
/// <summary>
/// Level of importance of what I'm logging. Defines logging severity levels.
/// similar to System.Diagnostics.EventLogEntryType
/// same as Microsoft.Extensions.Logging.LogLevel
/// </summary>
public enum LogLevel
{
/// <summary>
/// Logs that contain the most detailed messages. These messages may contain sensitive application data.
/// These messages are disabled by default and should never be enabled in a production environment.
/// </summary>
Trace = 0,
/// <summary>
/// Logs that are used for interactive investigation during development.
/// These logs should primarily contain information useful for debugging and have no long-term value.
/// </summary>
Debug = 1,
/// <summary>
/// Logs that track the general flow of the application. These logs should have long-term value.
/// </summary>
Information = 2,
/// <summary>
/// Logs that highlight an abnormal or unexpected event in the application flow,
/// but do not otherwise cause the application execution to stop.
/// </summary>
Warning = 3,
/// <summary>
/// Logs that highlight when the current flow of execution is stopped due to a failure.
/// These should indicate a failure in the current activity, not an application-wide failure.
/// </summary>
Error = 4,
/// <summary>
/// Logs that describe an unrecoverable application or system crash,
/// or a catastrophic failure that requires immediate attention.
/// </summary>
Critical = 5,
/// <summary>
/// Not used for writing log messages. Specifies that a logging category should not write any messages.
/// </summary>
None = 6,
}
/// <summary>
/// An entry to be logged. may be logged async to producer. (on another thread)
/// Assume time stamp is Now.
/// </summary>
public class LogEntryBase
{
public string Message = string.Empty; // Description of the event. NOTE: Use ToString() instead of this directly to get args.
public LogLevel LevelId = LogLevel.Information;
public int UserId = ValidState.kInvalidId; // id for a thread of work for this user/worker. GetCurrentThreadId() ?
public object? Detail; // extra information. that may be stored via ToString();
public LogEntryBase() // props to be populated later.
{ }
public LogEntryBase(string message, LogLevel levelId = LogLevel.Information, int userId = ValidState.kInvalidId, object? detail = null)
{
Message = message;
LevelId = levelId;
UserId = userId; // ValidState.IsValidId(userId) GetCurrentThreadId()
Detail = detail;
}
public override string ToString()
{
return Message;
}
}
/// <summary>
/// Emulate System.Diagnostics.WriteEntry
/// This can possibly be forwarded to NLog or Log4Net ? AKA Sink.
/// similar to Microsoft.Extensions.Logging.ILogger
/// NOTE: This is not async! Do any async stuff on another thread such that we don't really effect the caller.
/// </summary>
public interface ILogger
{
/// <summary>
/// Is this log message important enough to be logged?
/// </summary>
/// <param name="levelId"></param>
/// <returns></returns>
bool IsEnabled(LogLevel levelId = LogLevel.Information);
/// <summary>
/// Log this. assume will also check IsEnabled().
/// </summary>
/// <param name="entry"></param>
void LogEntry(LogEntryBase entry);
}
/// <summary>
/// Logging of events. base class.
/// Similar to System.Diagnostics.EventLog
/// NOTE: This is not async! Do any async stuff on another thread such that we don't really effect the caller.
/// </summary>
public class LoggerBase : ILogger
{
protected LogLevel _FilterLevel = LogLevel.Debug; // Only log stuff at this level and above in importance.
public LogLevel FilterLevel => _FilterLevel; // Only log stuff at this level and above in importance.
public void SetFilterLevel(LogLevel levelId)
{
// Only log stuff this important or better.
_FilterLevel = levelId;
}
public virtual bool IsEnabled(LogLevel levelId = LogLevel.Information) // ILogger
{
// ILogger Override this
// Quick filter check to see if this type is logged. Check this first if the rendering would be heavy.
return levelId >= _FilterLevel; // Log this?
}
/// <summary>
/// Separator after time prefix.
/// </summary>
/// <param name="levelId"></param>
/// <returns></returns>
public static string GetSeparator(LogLevel levelId)
{
switch (levelId)
{
case LogLevel.Warning: return ":?:";
case LogLevel.Error:
case LogLevel.Critical: return ":!:";
default:
return ":";
}
}
/// <summary>
/// ILogger Override this. default behavior = debug.
/// </summary>
/// <param name="entry"></param>
public virtual void LogEntry(LogEntryBase entry) // ILogger
{
if (!IsEnabled(entry.LevelId)) // ignore this?
return;
if (ValidState.IsValidId(entry.UserId))
{
}
if (entry.Detail != null)
{
}
System.Diagnostics.Debug.WriteLine(GetSeparator(entry.LevelId) + entry.ToString());
}
public void LogEntry(string message, LogLevel levelId = LogLevel.Information,
int userId = ValidState.kInvalidId,
object? detail = null)
{
LogEntry(new LogEntryBase(message, levelId, userId, detail));
}
public void info(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
// Helper.
LogEntry(message, LogLevel.Information, userId, detail);
}
public void warn(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
// Helper.
LogEntry(message, LogLevel.Warning, userId, detail);
}
public void debug(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
LogEntry(message, LogLevel.Debug, userId, detail);
}
public void trace(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
LogEntry(message, LogLevel.Trace, userId, detail);
}
public void error(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
LogEntry(message, LogLevel.Error, userId, detail);
}
public void fatal(string message, int userId = ValidState.kInvalidId, object? detail = null)
{
LogEntry(message, LogLevel.Critical, userId, detail);
}
/// <summary>
/// Do i want to log full detail for an Exception?
/// </summary>
/// <param name="levelId"></param>
/// <returns></returns>
public static bool IsExceptionDetailLogged(LogLevel levelId)
{
if (levelId >= LogLevel.Error) // Always keep stack trace etc for error.
return true;
// Is debug mode ?
return false;
}
/// <summary>
/// Helper for Special logging for exceptions.
/// </summary>
/// <param name="ex"></param>
/// <param name="levelId"></param>
/// <param name="userId"></param>
public virtual void LogException(Exception ex, LogLevel levelId = LogLevel.Error, int userId = ValidState.kInvalidId)
{
object? detail = null;
if (IsExceptionDetailLogged(levelId))
detail = ex;
LogEntry(ex.Message, LogLevel.Critical, userId, detail);
}
}
/// <summary>
/// Global static logging helper. Uses LogStart.
/// </summary>
public static class LoggerUtil
{
public static LoggerBase? LogStart; // always log fine detail at startup.
/// <summary>
/// Not officially logged. Just debug console. Like LogLevel.Debug.. Compile this out?
/// </summary>
/// <param name="message"></param>
/// <param name="userId"></param>
public static void DebugEntry(string message, int userId = ValidState.kInvalidId)
{
// startup ?
System.Diagnostics.Debug.WriteLine("Debug: " + message);
// System.Diagnostics.Trace.WriteLine();
if (LogStart != null)
{
LogStart.LogEntry(message, LogLevel.Debug, userId);
}
}
/// <summary>
/// an exception that I don't do anything about! NOT going to call LogException
/// set a break point here if we want.
/// </summary>
/// <param name="subject"></param>
/// <param name="ex">some Exception</param>
/// <param name="userId"></param>
public static void DebugError(string subject, Exception? ex = null, int userId = ValidState.kInvalidId)
{
System.Diagnostics.Debug.WriteLine("DebugException " + subject + ":" + ex?.Message);
// Console.WriteLine();
// System.Diagnostics.Trace.WriteLine();
if (LogStart != null)
{
LogStart.LogEntry(subject, LogLevel.Error, userId);
}
}
/// <summary>
/// this should never happen. Break point this!
/// </summary>
public static void DebugNEVER(string subject)
{
}
/// <summary>
/// Create a high detail log for startup.
/// For startup also check:
/// System Event Logger for Applications.
/// IIS web.config stdoutLogFile. <aspNetCore processPath=".\FourTeAdminWeb.exe" stdoutLogEnabled="true" stdoutLogFile="C:\FourTe\stdout" hostingModel="InProcess">
/// IIS wwwroot/logs/
/// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-3.1
/// </summary>
/// <param name="filePath"></param>
public static void CreateStartupLog(string filePath)
{
LogStart = new LogFileBase(filePath);
}
}
}