-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cs
111 lines (94 loc) · 3.79 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
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
public static class Log
{
static Log()
{
Initialize("[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}", false);
}
public static ILogger Instance => Serilog.Log.Logger;
public static void Initialize(string template, bool debug)
{
var theme = new SystemConsoleTheme(
new Dictionary<ConsoleThemeStyle, SystemConsoleThemeStyle>
{
[ConsoleThemeStyle.Invalid] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Yellow },
[ConsoleThemeStyle.Null] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Name] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.White },
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.DarkGray },
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Blue },
[ConsoleThemeStyle.LevelWarning] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Black, Background = ConsoleColor.Yellow },
[ConsoleThemeStyle.LevelError] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Black, Background = ConsoleColor.Red },
[ConsoleThemeStyle.LevelFatal] = new SystemConsoleThemeStyle { Foreground = ConsoleColor.Black, Background = ConsoleColor.Red },
}
);
var logger = new LoggerConfiguration()
.MinimumLevel.Is(debug ? LogEventLevel.Debug : LogEventLevel.Information)
.WriteTo.Console(
outputTemplate: template,
theme: theme
);
Serilog.Log.Logger = logger.CreateLogger();
}
public static void Disable()
{
Serilog.Log.Logger = Logger.None;
}
public static void Cleanup()
{
Serilog.Log.CloseAndFlush();
}
public static async Task TryDebug(Func<Task<object?>> messageFunc)
{
if (!Serilog.Log.Logger.IsEnabled(LogEventLevel.Debug))
{
return;
}
if (await messageFunc() is object message)
{
Debug(message);
}
}
public static void TryDebug(Func<object?> messageFunc)
{
if (!Serilog.Log.Logger.IsEnabled(LogEventLevel.Debug))
{
return;
}
if (messageFunc() is object message)
{
Debug(message);
}
}
public static void Debug(object message)
{
Serilog.Log.Logger.Debug(message.ToString() ?? "");
}
public static void Info(object message)
{
Serilog.Log.Logger.Information(message.ToString() ?? "");
}
public static void Warn(object message)
{
Serilog.Log.Logger.Warning(message.ToString() ?? "");
}
public static void Warn(object message, Exception e)
{
Serilog.Log.Logger.Warning(e, message.ToString() ?? "");
}
public static void Error(object message)
{
Serilog.Log.Logger.Error(message.ToString() ?? "");
}
public static void Error(object message, Exception e)
{
Serilog.Log.Logger.Error(e, message.ToString() ?? "");
}
}