|
1 | 1 | using System;
|
2 |
| -using System.Collections.Concurrent; |
3 |
| -using System.Reflection; |
4 |
| -using System.Runtime.InteropServices; |
5 | 2 | using Dotnet.Script.DependencyModel.Logging;
|
6 | 3 | using Microsoft.Extensions.Logging;
|
7 | 4 | using Microsoft.Extensions.Logging.Console;
|
8 |
| -using Microsoft.Extensions.Logging.Console.Internal; |
| 5 | +using Microsoft.Extensions.Options; |
9 | 6 | using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
10 | 7 |
|
11 | 8 | namespace Dotnet.Script
|
12 | 9 | {
|
13 | 10 | public static class LogHelper
|
14 |
| - { |
| 11 | + { |
15 | 12 | public static LogFactory CreateLogFactory(string verbosity)
|
16 | 13 | {
|
17 | 14 | var logLevel = (LogLevel)LevelMapper.FromString(verbosity);
|
18 |
| - |
19 |
| - var loggerFactory = new LoggerFactory(); |
20 | 15 |
|
21 |
| - loggerFactory.AddProvider(new ConsoleErrorLoggerProvider((message, level) => level >= logLevel)); |
| 16 | + var loggerFilterOptions = new LoggerFilterOptions() { MinLevel = logLevel }; |
| 17 | + |
| 18 | + var consoleLoggerProvider = new ConsoleLoggerProvider(new ConsoleOptionsMonitor()); |
| 19 | + |
| 20 | + var loggerFactory = new LoggerFactory(new[] { consoleLoggerProvider }, loggerFilterOptions); |
22 | 21 |
|
23 | 22 | return type =>
|
24 | 23 | {
|
25 | 24 | var logger = loggerFactory.CreateLogger(type);
|
26 | 25 | return (level, message, exception) =>
|
27 | 26 | {
|
28 |
| - logger.Log((LogLevel)level, message, exception); |
| 27 | + logger.Log((LogLevel)level, message, exception); |
29 | 28 | };
|
30 | 29 | };
|
31 |
| - } |
32 |
| - } |
33 |
| - |
34 |
| - public class WindowsLogErrorConsole : IConsole |
35 |
| - { |
36 |
| - private void SetColor(ConsoleColor? background, ConsoleColor? foreground) |
37 |
| - { |
38 |
| - if (background.HasValue) |
39 |
| - { |
40 |
| - Console.BackgroundColor = background.Value; |
41 |
| - } |
42 |
| - |
43 |
| - if (foreground.HasValue) |
44 |
| - { |
45 |
| - Console.ForegroundColor = foreground.Value; |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - private void ResetColor() |
50 |
| - { |
51 |
| - Console.ResetColor(); |
52 |
| - } |
53 |
| - |
54 |
| - public void Write(string message, ConsoleColor? background, ConsoleColor? foreground) |
55 |
| - { |
56 |
| - SetColor(background, foreground); |
57 |
| - Console.Error.Write(message); |
58 |
| - ResetColor(); |
59 |
| - } |
60 |
| - |
61 |
| - public void WriteLine(string message, ConsoleColor? background, ConsoleColor? foreground) |
62 |
| - { |
63 |
| - SetColor(background, foreground); |
64 |
| - Console.Error.WriteLine(message); |
65 |
| - ResetColor(); |
66 |
| - } |
67 |
| - |
68 |
| - public void Flush() |
69 |
| - { |
70 |
| - // No action required as for every write, data is sent directly to the console |
71 |
| - // output stream |
72 | 30 | }
|
73 | 31 | }
|
74 | 32 |
|
75 |
| - public class AnsiSystemErrorConsole : IAnsiSystemConsole |
| 33 | + internal class ConsoleOptionsMonitor : IOptionsMonitor<ConsoleLoggerOptions> |
76 | 34 | {
|
77 |
| - public void Write(string message) |
78 |
| - { |
79 |
| - System.Console.Error.Write(message); |
80 |
| - } |
| 35 | + private ConsoleLoggerOptions _consoleLoggerOptions; |
81 | 36 |
|
82 |
| - public void WriteLine(string message) |
| 37 | + public ConsoleOptionsMonitor() |
83 | 38 | {
|
84 |
| - System.Console.Error.WriteLine(message); |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - public class ConsoleErrorLoggerProvider : ILoggerProvider |
89 |
| - { |
90 |
| - private readonly Func<string, LogLevel, bool> _filter; |
91 |
| - |
92 |
| - private readonly ConcurrentDictionary<string, ConsoleLogger> _loggers = new ConcurrentDictionary<string, ConsoleLogger>(); |
93 |
| - |
94 |
| - private readonly ConsoleLoggerProcessor _messageQueue = new ConsoleLoggerProcessor(); |
95 |
| - |
96 |
| - private readonly static ConstructorInfo ConsoleLoggerConstructor = typeof(ConsoleLogger).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0]; |
97 |
| - |
98 |
| - public ConsoleErrorLoggerProvider(Func<string, LogLevel, bool> filter) |
99 |
| - { |
100 |
| - _filter = filter; |
101 |
| - } |
102 |
| - |
103 |
| - public ILogger CreateLogger(string name) |
104 |
| - { |
105 |
| - return _loggers.GetOrAdd(name, CreateLoggerImplementation); |
| 39 | + _consoleLoggerOptions = new ConsoleLoggerOptions() |
| 40 | + { |
| 41 | + LogToStandardErrorThreshold = LogLevel.Trace |
| 42 | + }; |
106 | 43 | }
|
107 | 44 |
|
108 |
| - private ConsoleLogger CreateLoggerImplementation(string name) |
109 |
| - { |
110 |
| - var consoleLogger = (ConsoleLogger)ConsoleLoggerConstructor.Invoke(new object[] { name, _filter, null, _messageQueue }); |
111 |
| - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
112 |
| - { |
113 |
| - consoleLogger.Console = new WindowsLogErrorConsole(); |
114 |
| - } |
115 |
| - else |
116 |
| - { |
117 |
| - consoleLogger.Console = new AnsiLogConsole(new AnsiSystemErrorConsole()); |
118 |
| - } |
| 45 | + public ConsoleLoggerOptions CurrentValue => _consoleLoggerOptions; |
119 | 46 |
|
120 |
| - return consoleLogger; |
121 |
| - } |
| 47 | + public ConsoleLoggerOptions Get(string name) => _consoleLoggerOptions; |
122 | 48 |
|
123 |
| - public void Dispose() |
| 49 | + public IDisposable OnChange(Action<ConsoleLoggerOptions, string> listener) |
124 | 50 | {
|
125 |
| - _messageQueue.Dispose(); |
| 51 | + return null; |
126 | 52 | }
|
127 |
| - } |
| 53 | + } |
128 | 54 | }
|
0 commit comments