-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSpectreInlineLogger.cs
More file actions
57 lines (46 loc) · 2.15 KB
/
Copy pathSpectreInlineLogger.cs
File metadata and controls
57 lines (46 loc) · 2.15 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
using System;
using System.Text;
using Microsoft.Extensions.Logging;
namespace Spectre.Console.Extensions.Logging
{
public class SpectreInlineLogger : BaseSpectreLogger{
public SpectreInlineLogger(string name, SpectreConsoleLoggerConfiguration config) : base(name, config) { }
protected override string RenderLogMessage<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var formattedLine = formatter(state, exception);
var sb = new StringBuilder();
if (Config.IncludePrefix)
{
var prefix = GetLevelMarkup(logLevel);
sb.Append(prefix);
}
if (Config.IncludeEventId)
{
var eventIdMarkup = GetEventIdMarkup(eventId);
sb.Append(eventIdMarkup);
}
sb.Append(formattedLine);
return sb.ToString();
}
private static string GetEventIdMarkup(EventId eventId) => eventId.Id == 0 ? EmptyEventIdMarkup : string.Format(EventIdMarkup, eventId.Id);
private const string EmptyEventIdMarkup = " ";
private const string EventIdMarkup = "[dim grey][[{0,4:####}]][/] ";
private const string Unknown = "| UNKN|: ";
private const string Trace = "[dim grey]|TRACE|:[/] ";
private const string Debug = "[dim]|DEBUG|:[/] ";
private const string Information = "[bold dim]| INFO|:[/] ";
private const string Warning = "[bold orange3]| WARN|:[/] ";
private const string Error = "[bold red]|ERROR|:[/] ";
private const string Critical = "[bold underline white on red]| CRIT|:[/] ";
private static string GetLevelMarkup(LogLevel level) =>
level switch {
LogLevel.Critical => Critical,
LogLevel.Error => Error,
LogLevel.Warning => Warning,
LogLevel.Information => Information,
LogLevel.Debug => Debug,
LogLevel.Trace => Trace,
_ => Unknown
};
}
}