forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptConsoleLogger.cs
More file actions
147 lines (127 loc) · 4.9 KB
/
ScriptConsoleLogger.cs
File metadata and controls
147 lines (127 loc) · 4.9 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using ScriptCs.Contracts;
using LogLevel = ScriptCs.Contracts.LogLevel;
namespace ScriptCs.Hosting
{
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
public class ScriptConsoleLogger : Common.Logging.Factory.AbstractLogger
{
private readonly LogLevel _consoleLogLevel;
private readonly IConsole _console;
private readonly Common.Logging.ILog _log;
private readonly Dictionary<Common.Logging.LogLevel, ConsoleColor> colors =
new Dictionary<Common.Logging.LogLevel, ConsoleColor>
{
{ Common.Logging.LogLevel.Fatal, ConsoleColor.Red },
{ Common.Logging.LogLevel.Error, ConsoleColor.DarkRed },
{ Common.Logging.LogLevel.Warn, ConsoleColor.DarkYellow },
{ Common.Logging.LogLevel.Info, ConsoleColor.Gray },
{ Common.Logging.LogLevel.Debug, ConsoleColor.DarkGray },
{ Common.Logging.LogLevel.Trace, ConsoleColor.DarkMagenta },
};
public ScriptConsoleLogger(LogLevel consoleLogLevel, IConsole console, Common.Logging.ILog log)
{
Guard.AgainstNullArgument("console", console);
Guard.AgainstNullArgument("log", log);
_consoleLogLevel = consoleLogLevel;
_console = console;
_log = log;
}
public override bool IsFatalEnabled
{
get { return true; }
}
public override bool IsErrorEnabled
{
get { return true; }
}
public override bool IsWarnEnabled
{
get { return true; }
}
public override bool IsInfoEnabled
{
get { return _log.IsInfoEnabled || _consoleLogLevel != LogLevel.Error; }
}
public override bool IsDebugEnabled
{
get { return _log.IsDebugEnabled || _consoleLogLevel == LogLevel.Debug || _consoleLogLevel == LogLevel.Trace; }
}
public override bool IsTraceEnabled
{
get { return _log.IsTraceEnabled || _consoleLogLevel == LogLevel.Trace; }
}
protected override void WriteInternal(Common.Logging.LogLevel level, object message, Exception exception)
{
Guard.AgainstNullArgument("message", message);
var consoleLog = false;
switch (level)
{
case Common.Logging.LogLevel.Fatal:
consoleLog = true;
if (_log.IsFatalEnabled)
{
_log.Fatal(message, exception);
}
break;
case Common.Logging.LogLevel.Error:
consoleLog = true;
if (_log.IsErrorEnabled)
{
_log.Error(message, exception);
}
break;
case Common.Logging.LogLevel.Warn:
consoleLog = true;
if (_log.IsWarnEnabled)
{
_log.Warn(message, exception);
}
break;
case Common.Logging.LogLevel.Info:
consoleLog = _consoleLogLevel != LogLevel.Error;
if (_log.IsInfoEnabled)
{
_log.Info(message, exception);
}
break;
case Common.Logging.LogLevel.Debug:
consoleLog = _consoleLogLevel == LogLevel.Debug || _consoleLogLevel == LogLevel.Trace;
if (_log.IsDebugEnabled)
{
_log.Debug(message, exception);
}
break;
case Common.Logging.LogLevel.Trace:
consoleLog = _consoleLogLevel == LogLevel.Trace;
if (_log.IsTraceEnabled)
{
_log.Trace(message, exception);
}
break;
}
if (consoleLog)
{
var prefix = level == Common.Logging.LogLevel.Info
? null
: string.Concat(level.ToString().ToUpperInvariant(), ": ");
ConsoleColor color;
if (!colors.TryGetValue(level, out color))
{
color = ConsoleColor.White;
}
var originalColor = _console.ForegroundColor;
_console.ForegroundColor = color;
try
{
_console.WriteLine(string.Concat(prefix, message.ToString()));
}
finally
{
_console.ForegroundColor = originalColor;
}
}
}
}
}