-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathCustomLoggerProvider.cs
More file actions
120 lines (104 loc) · 4.28 KB
/
CustomLoggerProvider.cs
File metadata and controls
120 lines (104 loc) · 4.28 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
/// <summary>
/// LoggerProvider to generate Custom Log message
/// </summary>
public class CustomLoggerProvider : ILoggerProvider
{
private readonly LogLevel _minimumLogLevel;
public CustomLoggerProvider(LogLevel minimumLogLevel = LogLevel.Information)
{
_minimumLogLevel = minimumLogLevel;
}
public void Dispose() { }
/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
return new CustomConsoleLogger(_minimumLogLevel);
}
public class CustomConsoleLogger : ILogger
{
// Minimum LogLevel. LogLevel below this would be disabled.
private readonly LogLevel _minimumLogLevel;
public CustomConsoleLogger(LogLevel minimumLogLevel = LogLevel.Information)
{
_minimumLogLevel = minimumLogLevel;
}
// Color values based on LogLevel
// LogLevel Foreground Background
// Trace White Black
// Debug White Gray
// Information Green Black
// Warning Yellow Black
// Error White Red
// Critical White DarkRed
/// <summary>
/// Foreground color for Console message based on LogLevel
/// </summary>
Dictionary<LogLevel, ConsoleColor> _logLevelToForeGroundConsoleColorMap = new()
{
{LogLevel.Trace, ConsoleColor.White},
{LogLevel.Debug, ConsoleColor.White},
{LogLevel.Information, ConsoleColor.Green},
{LogLevel.Warning, ConsoleColor.Yellow},
{LogLevel.Error, ConsoleColor.White},
{LogLevel.Critical, ConsoleColor.White}
};
/// <summary>
/// Background color for Console message based on LogLevel
/// </summary>
Dictionary<LogLevel, ConsoleColor> _logLevelToBackGroundConsoleColorMap = new()
{
{LogLevel.Trace, ConsoleColor.Black},
{LogLevel.Debug, ConsoleColor.Gray},
{LogLevel.Information, ConsoleColor.Black},
{LogLevel.Warning, ConsoleColor.Black},
{LogLevel.Error, ConsoleColor.Red},
{LogLevel.Critical, ConsoleColor.DarkRed}
};
/// <summary>
/// Maps LogLevel to abbreviated labels matching ASP.NET Core's default console formatter.
/// </summary>
private static readonly Dictionary<LogLevel, string> _logLevelToAbbreviation = new()
{
{LogLevel.Trace, "trce"},
{LogLevel.Debug, "dbug"},
{LogLevel.Information, "info"},
{LogLevel.Warning, "warn"},
{LogLevel.Error, "fail"},
{LogLevel.Critical, "crit"}
};
/// <summary>
/// Creates Log message by setting console message color based on LogLevel.
/// </summary>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
if (!_logLevelToAbbreviation.TryGetValue(logLevel, out string? abbreviation))
{
return;
}
ConsoleColor originalForeGroundColor = Console.ForegroundColor;
ConsoleColor originalBackGroundColor = Console.BackgroundColor;
Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.White);
Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.Black);
Console.Write($"{abbreviation}:");
Console.ForegroundColor = originalForeGroundColor;
Console.BackgroundColor = originalBackGroundColor;
Console.WriteLine($" {formatter(state, exception)}");
}
/// <inheritdoc/>
public bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None && logLevel >= _minimumLogLevel;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
throw new NotImplementedException();
}
}
}