-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
58 lines (56 loc) · 2.08 KB
/
Log.cs
File metadata and controls
58 lines (56 loc) · 2.08 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
using System.IO;
using static System.DateTime;
namespace Webtech3;
/// <summary>
/// Logger class.
/// </summary>
public static class Logger {
private enum Level : byte {Trace, Info, Warning, Error, Critical, Fatal}
private static readonly string path;
static Logger() {
path = Path.GetDirectoryName(
Environment.ProcessPath ?? Environment.CurrentDirectory
) + '/';
path += Today.ToShortDateString() + ".log";
}
private static void Message(Level lvl, string src, params object[] entries) {
var mes = $"{Now} ({src}) [{lvl}]: - " + string.Join(' ',
entries.Select(
e => e.ToString()
)
) + '\n';
File.AppendAllText(path, mes);
Console.ForegroundColor = lvl switch {
Level.Trace => ConsoleColor.Gray,
Level.Info => ConsoleColor.White,
Level.Warning => ConsoleColor.Yellow,
Level.Error => ConsoleColor.Red,
Level.Critical => ConsoleColor.Cyan,
Level.Fatal => ConsoleColor.Magenta,
_ => ConsoleColor.Black
};
Console.Write(mes);
Console.ResetColor();
}
/// <summary>
/// Property for getting cwd path
/// </summary>
/// <value>String with cwd abs path</value>
public static string WorkingDir {
get => Path.GetDirectoryName(path)!;
}
public static void Trace(string src, params object[] entries)
=> Message(Level.Trace, src, entries);
public static void Info(string src, params object[] entries)
=> Message(Level.Info, src, entries);
public static void Warning(string src, params object[] entries)
=> Message(Level.Warning, src, entries);
public static void Error(string src, params object[] entries)
=> Message(Level.Error, src, entries);
public static void Critical(string src, params object[] entries)
=> Message(Level.Critical, src, entries);
public static void Fatal(string src, params object[] entries) {
Message(Level.Fatal, src, entries);
Environment.Exit(1);
}
}