-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomConsoleFormatter.cs
More file actions
44 lines (40 loc) · 1.46 KB
/
CustomConsoleFormatter.cs
File metadata and controls
44 lines (40 loc) · 1.46 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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
namespace FalconFlipMerchantManager
{
public class CustomConsoleFormatter : ConsoleFormatter
{
public CustomConsoleFormatter() : base("custom") { }
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
var originalColor = Console.ForegroundColor;
try
{
Console.ForegroundColor = GetConsoleColor(logEntry.LogLevel);
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
}
}
finally
{
Console.ForegroundColor = originalColor;
}
}
private ConsoleColor GetConsoleColor(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Critical => ConsoleColor.Red,
LogLevel.Error => ConsoleColor.Red,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Information => ConsoleColor.White,
LogLevel.Debug => ConsoleColor.Gray,
LogLevel.Trace => ConsoleColor.DarkGray,
_ => Console.ForegroundColor
};
}
}
}