-
Notifications
You must be signed in to change notification settings - Fork 46
/
StaticContextAppender.cs
120 lines (100 loc) · 3.71 KB
/
StaticContextAppender.cs
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
namespace ServiceControl.AcceptanceTesting
{
using System;
using System.Diagnostics;
using System.Reflection;
using NServiceBus.AcceptanceTesting;
using NServiceBus.Logging;
class StaticContextAppender : ILog
{
public bool IsDebugEnabled => StaticLoggerFactory.CurrentContext.LogLevel <= LogLevel.Debug;
public bool IsInfoEnabled => StaticLoggerFactory.CurrentContext.LogLevel <= LogLevel.Info;
public bool IsWarnEnabled => StaticLoggerFactory.CurrentContext.LogLevel <= LogLevel.Warn;
public bool IsErrorEnabled => StaticLoggerFactory.CurrentContext.LogLevel <= LogLevel.Error;
public bool IsFatalEnabled => StaticLoggerFactory.CurrentContext.LogLevel <= LogLevel.Fatal;
public void Debug(string message)
{
Log(message, LogLevel.Debug);
}
public void Debug(string message, Exception exception)
{
var fullMessage = $"{message} {exception}";
Log(fullMessage, LogLevel.Debug);
}
public void DebugFormat(string format, params object[] args)
{
var fullMessage = string.Format(format, args);
Log(fullMessage, LogLevel.Debug);
}
public void Info(string message)
{
Log(message, LogLevel.Info);
}
public void Info(string message, Exception exception)
{
var fullMessage = $"{message} {exception}";
Log(fullMessage, LogLevel.Info);
}
public void InfoFormat(string format, params object[] args)
{
var fullMessage = string.Format(format, args);
Log(fullMessage, LogLevel.Info);
}
public void Warn(string message)
{
Log(message, LogLevel.Warn);
}
public void Warn(string message, Exception exception)
{
var fullMessage = $"{message} {exception}";
Log(fullMessage, LogLevel.Warn);
}
public void WarnFormat(string format, params object[] args)
{
var fullMessage = string.Format(format, args);
Log(fullMessage, LogLevel.Warn);
}
public void Error(string message)
{
Log(message, LogLevel.Error);
}
public void Error(string message, Exception exception)
{
var fullMessage = $"{message} {exception}";
Log(fullMessage, LogLevel.Error);
}
public void ErrorFormat(string format, params object[] args)
{
var fullMessage = string.Format(format, args);
Log(fullMessage, LogLevel.Error);
}
public void Fatal(string message)
{
Log(message, LogLevel.Fatal);
}
public void Fatal(string message, Exception exception)
{
var fullMessage = $"{message} {exception}";
Log(fullMessage, LogLevel.Fatal);
}
public void FatalFormat(string format, params object[] args)
{
var fullMessage = string.Format(format, args);
Log(fullMessage, LogLevel.Fatal);
}
void Log(string message, LogLevel messageSeverity)
{
if (StaticLoggerFactory.CurrentContext.LogLevel > messageSeverity)
{
return;
}
Trace.WriteLine(message);
StaticLoggerFactory.CurrentContext.Logs.Enqueue(new ScenarioContext.LogItem
{
Endpoint = (string)typeof(ScenarioContext).GetProperty("CurrentEndpoint", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(StaticLoggerFactory.CurrentContext),
Level = messageSeverity,
Message = message
});
}
}
}