This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
LoupeLogProviderLoggingTests.cs
215 lines (183 loc) · 7.96 KB
/
LoupeLogProviderLoggingTests.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#if !NET452
namespace LibLog.Logging.LogProviders
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Gibraltar.Agent;
using Gibraltar.Agent.Data;
using Shouldly;
using Xunit;
using YourRootNamespace.Logging;
using YourRootNamespace.Logging.LogProviders;
using ILog = YourRootNamespace.Logging.ILog;
public class LoupeProviderLoggingTests : IDisposable
{
private readonly ILog _sut;
private readonly MessageTester _messageTester;
public LoupeProviderLoggingTests()
{
_sut = new LoggerExecutionWrapper(new LoupeLogProvider().GetLogger("Test"));
_messageTester = new MessageTester();
}
public void Dispose()
{
//this is debatably sketchy since we're not likely to be really shutting down, but otherwise we can hang the unit tests.
Log.EndSession();
_messageTester.Dispose();
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "CRITICAL")] //Fatal messages in Loupe are rendered as Critical
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "DEBUG")] //Trace messages in Loupe are rendered as Debug
[InlineData(LogLevel.Warn, "WARN")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Should_be_able_to_log_message(LogLevel logLevel, string messagePrefix)
{
_messageTester.Reset();
_sut.Log(logLevel, () => messagePrefix + " log message");
_messageTester.WaitForMessages();
_messageTester.Message.Count.ShouldBe(1);
var firstMessage = _messageTester.Message.First();
firstMessage.Caption.ShouldBe(messagePrefix + " log message");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "CRITICAL")] //Fatal messages in Loupe are rendered as Critical
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "DEBUG")] //Trace messages in Loupe are rendered as Debug
[InlineData(LogLevel.Warn, "WARN")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Should_be_able_to_log_message_and_exception(LogLevel logLevel, string messagePrefix)
{
_messageTester.Reset();
_sut.Log(logLevel, () => messagePrefix + " log message with exception", new Exception("e"));
_messageTester.WaitForMessages();
_messageTester.Message.Count.ShouldBe(1);
var firstMessage = _messageTester.Message.First();
firstMessage.Caption.ShouldBe(messagePrefix + " log message with exception");
firstMessage.HasException.ShouldBeTrue();
firstMessage.Exception.ShouldNotBeNull();
firstMessage.Exception.Message.ShouldBe("e");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "CRITICAL")] //Fatal messages in Loupe are rendered as Critical
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "DEBUG")] //Trace messages in Loupe are rendered as Debug
[InlineData(LogLevel.Warn, "WARN")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Should_be_able_to_log_message_with_params(LogLevel logLevel, string messagePrefix)
{
_messageTester.Reset();
_sut.Log(logLevel, () => messagePrefix + " log message {0}", null, "replaced");
_messageTester.WaitForMessages();
_messageTester.Message.Count.ShouldBe(1);
var firstMessage = _messageTester.Message.First();
firstMessage.Caption.ShouldBe(messagePrefix + " log message replaced");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "CRITICAL")] //Fatal messages in Loupe are rendered as Critical
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "DEBUG")] //Trace messages in Loupe are rendered as Debug
[InlineData(LogLevel.Warn, "WARN")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Should_be_able_to_log_message_and_exception_with_formatparams(LogLevel logLevel, string messagePrefix)
{
_messageTester.Reset();
_sut.Log(logLevel, () => messagePrefix + " log message {abc} with exception", new Exception("e"), "replaced");
_messageTester.WaitForMessages();
_messageTester.Message.Count.ShouldBe(1);
var firstMessage = _messageTester.Message.First();
firstMessage.Caption.ShouldBe(messagePrefix + " log message replaced with exception");
firstMessage.HasException.ShouldBeTrue();
firstMessage.Exception.ShouldNotBeNull();
firstMessage.Exception.Message.ShouldBe("e");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "CRITICAL")] //Fatal messages in Loupe are rendered as Critical
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "DEBUG")] //Trace messages in Loupe are rendered as Debug
[InlineData(LogLevel.Warn, "WARN")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Should_be_able_to_log_message_and_exception_with_formatparams_modifiers(LogLevel logLevel, string messagePrefix)
{
_messageTester.Reset();
_sut.Log(logLevel, () => messagePrefix + " log message {@abc} with exception", new Exception("e"), "replaced");
_messageTester.WaitForMessages();
_messageTester.Message.Count.ShouldBe(1);
var firstMessage = _messageTester.Message.First();
firstMessage.Caption.ShouldBe(messagePrefix + " log message replaced with exception");
firstMessage.HasException.ShouldBeTrue();
firstMessage.Exception.ShouldNotBeNull();
firstMessage.Exception.Message.ShouldBe("e");
}
[Fact]
public void Can_check_is_log_level_enabled()
{
_sut.AssertCanCheckLogLevelsEnabled();
}
/// <summary>
/// Aggregates messages as they are generated so we can verify the logging results
/// </summary>
private class MessageTester : IDisposable
{
private List<ILogMessage> _message;
public MessageTester()
{
Log.MessagePublished += LogOnMessage;
}
public List<ILogMessage> Message
{
get
{
lock (this)
{
return _message;
}
}
}
/// <summary>
/// Wait for any messages in the queue to commit.
/// </summary>
public void WaitForMessages()
{
Gibraltar.Monitor.Log.Flush();
Task.Delay(250).GetAwaiter().GetResult(); // TODO: do better signalling with a task.
}
/// <summary>
/// Reset the buffer and counts
/// </summary>
public void Reset()
{
WaitForMessages();
lock (this)
{
_message = new List<ILogMessage>();
}
}
public void Dispose()
{
Log.MessagePublished -= LogOnMessage;
}
private void LogOnMessage(object sender, LogMessageEventArgs e)
{
lock (this)
{
_message.AddRange(e.Messages);
}
}
}
}
}
#endif