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
/
Log4NetLogProviderLoggingTests.cs
182 lines (156 loc) · 6.72 KB
/
Log4NetLogProviderLoggingTests.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
namespace LibLog.Logging.LogProviders
{
using System;
using System.Linq;
using System.Reflection;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using Shouldly;
using Xunit;
using YourRootNamespace.Logging;
using YourRootNamespace.Logging.LogProviders;
using ILog = YourRootNamespace.Logging.ILog;
public class Log4NetLogProviderLoggingTests : IDisposable
{
private readonly MemoryAppender _memoryAppender;
private readonly ILog _sut;
private readonly ILogProvider _logProvider;
public Log4NetLogProviderLoggingTests()
{
_memoryAppender = new MemoryAppender();
var repository = LogManager.GetRepository(Assembly.GetAssembly(typeof(LogManager)));
BasicConfigurator.Configure(repository, _memoryAppender);
_logProvider = new Log4NetLogProvider();
_sut = new LoggerExecutionWrapper(new Log4NetLogProvider().GetLogger("Test"));
((log4net.Repository.Hierarchy.Hierarchy)repository).Root.Level = Level.All;
}
public void Dispose()
{
LogManager.Shutdown();
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "FATAL")]
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "ALL")] //Trace messages in log4net are rendered as ALL
[InlineData(LogLevel.Warn, "WARN")]
public void Should_be_able_to_log_message(LogLevel logLevel, string messagePrefix)
{
_sut.Log(logLevel, () => "m");
GetSingleMessage().ShouldBe(messagePrefix + "|m|");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "FATAL")]
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "ALL")] //Trace messages in log4net are rendered as ALL
[InlineData(LogLevel.Warn, "WARN")]
public void Should_be_able_to_log_message_and_exception(LogLevel logLevel, string messagePrefix)
{
_sut.Log(logLevel, () => "m", new Exception("e"));
GetSingleMessage().ShouldBe(messagePrefix + "|m|e");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "FATAL")]
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "ALL")] //Trace messages in log4net are rendered as ALL
[InlineData(LogLevel.Warn, "WARN")]
public void Should_be_able_to_log_message_with_formatParams(LogLevel logLevel, string messagePrefix)
{
_sut.Log(logLevel, () => "m {0}", null, "replaced");
GetSingleMessage().ShouldBe(messagePrefix + "|m replaced|");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "FATAL")]
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "ALL")] //Trace messages in log4net are rendered as ALL
[InlineData(LogLevel.Warn, "WARN")]
public void Should_be_able_to_log_message_and_exception_with_formatParams(LogLevel logLevel, string messagePrefix)
{
_sut.Log(logLevel, () => "m {abc}", new Exception("e"), "replaced");
GetSingleMessage().ShouldBe(messagePrefix + "|m replaced|e");
}
[Theory]
[InlineData(LogLevel.Debug, "DEBUG")]
[InlineData(LogLevel.Error, "ERROR")]
[InlineData(LogLevel.Fatal, "FATAL")]
[InlineData(LogLevel.Info, "INFO")]
[InlineData(LogLevel.Trace, "ALL")] //Trace messages in log4net are rendered as ALL
[InlineData(LogLevel.Warn, "WARN")]
public void Should_be_able_to_log_message_and_exception_with_formatParams_modifiers(LogLevel logLevel, string messagePrefix)
{
_sut.Log(logLevel, () => "m {@abc}", new Exception("e"), "replaced");
GetSingleMessage().ShouldBe(messagePrefix + "|m replaced|e");
}
[Fact]
public void Can_check_is_log_level_enabled()
{
_sut.AssertCanCheckLogLevelsEnabled();
}
[Fact]
public void Can_open_nested_diagnostics_context()
{
using (_logProvider.OpenNestedContext("context"))
{
_sut.Info("m");
var loggingEvent = _memoryAppender.GetEvents().Single();
loggingEvent.Properties.GetKeys().ShouldContain("NDC");
loggingEvent.Properties["NDC"].ShouldBe("context");
}
}
[Fact]
public void Can_open_mapped_diagnostics_context()
{
using (_logProvider.OpenMappedContext("key", "value"))
{
_sut.Info("m");
var loggingEvent = _memoryAppender.GetEvents().Single();
loggingEvent.Properties.GetKeys().ShouldContain("key");
loggingEvent.Properties["key"].ShouldBe("value");
}
}
[Fact]
public void Can_open_mapped_diagnostics_context_destructured()
{
var context = new MyMappedContext();
using (_logProvider.OpenMappedContext("key", context, true))
{
_sut.Info("m");
var loggingEvent = _memoryAppender.GetEvents().Single();
loggingEvent.Properties.GetKeys().ShouldContain("key");
loggingEvent.Properties["key"].ShouldBe("World");
}
}
[Fact]
public void Can_open_mapped_diagnostics_context_not_destructured()
{
var context = new MyMappedContext();
using (_logProvider.OpenMappedContext("key", context, false))
{
_sut.Info("m");
var loggingEvent = _memoryAppender.GetEvents().Single();
loggingEvent.Properties.GetKeys().ShouldContain("key");
loggingEvent.Properties["key"].ShouldBe("World");
}
}
[Fact]
public void Should_log_message_with_curly_brackets()
{
_sut.Log(LogLevel.Debug, () => "Query language substitutions: {'true'='1', 'false'='0', 'yes'=''Y'', 'no'=''N''}");
GetSingleMessage().ShouldContain("DEBUG|Query language substitutions: {'true'='1', 'false'='0', 'yes'=''Y'', 'no'=''N''}");
}
private string GetSingleMessage()
{
LoggingEvent loggingEvent = _memoryAppender.GetEvents().Single();
return $"{loggingEvent.Level}|{loggingEvent.MessageObject}|{loggingEvent.ExceptionObject?.Message ?? string.Empty}";
}
}
}