-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathCustomLoggerProvider.cs
More file actions
114 lines (101 loc) · 4.21 KB
/
CustomLoggerProvider.cs
File metadata and controls
114 lines (101 loc) · 4.21 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
// 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
{
public void Dispose() { }
/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
return new CustomConsoleLogger();
}
public class CustomConsoleLogger : ILogger
{
// Minimum LogLevel. LogLevel below this would be disabled.
private readonly LogLevel _minimumLogLevel = LogLevel.Information;
// 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.
/// Skips logging when in MCP stdio mode to keep stdout clean for JSON-RPC protocol.
/// </summary>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
// In MCP stdio mode, suppress all CLI logging to keep stdout clean for JSON-RPC.
if (Cli.Utils.IsMcpStdioMode)
{
return;
}
if (!IsEnabled(logLevel) || logLevel < _minimumLogLevel)
{
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 true;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
throw new NotImplementedException();
}
}
}