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
115 lines (98 loc) · 3.49 KB
/
ScriptConsoleLogger.cs
File metadata and controls
115 lines (98 loc) · 3.49 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
using Common.Logging;
using Common.Logging.Factory;
using ScriptCs.Contracts;
using LogLevel = ScriptCs.Contracts.LogLevel;
namespace ScriptCs.Hosting
{
public class ScriptConsoleLogger : AbstractLogger
{
private readonly LogLevel _consoleLogLevel;
private readonly IConsole _console;
private readonly ILog _log;
public ScriptConsoleLogger(LogLevel consoleLogLevel, IConsole console, 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, System.Exception exception)
{
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)
{
_console.WriteLine(string.Concat(level.ToString().ToUpperInvariant(), ": ", message.ToString()));
}
}
}
}