Skip to content

Commit ac81f8e

Browse files
Lyrexscott-xu
authored andcommitted
Implement more tests for NLog and Log4Net to increase coverage (#42)
1 parent d4f62eb commit ac81f8e

File tree

3 files changed

+484
-64
lines changed

3 files changed

+484
-64
lines changed

src/Ninject.Extensions.Logging.Log4Net.Test/Log4netTests.cs

Lines changed: 178 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,46 @@ static Log4netTests()
4141
// Configure the default repository
4242
var root = ((Hierarchy)log4net.LogManager.GetRepository(Assembly.GetCallingAssembly())).Root;
4343
root.AddAppender(testAppender);
44+
root.Level = Level.Trace;
4445
root.Repository.Configured = true;
4546
}
4647

48+
/// <summary>
49+
/// Tests trace logging
50+
/// </summary>
51+
[Fact]
52+
public void LogTrace()
53+
{
54+
this.TestLog(Level.Trace, typeof(CtorPropertyLoggerClass).GetMethod("LogTrace"));
55+
}
56+
57+
/// <summary>
58+
/// Tests trace logging with arguments
59+
/// </summary>
60+
[Fact]
61+
public void LogTraceWithArguments()
62+
{
63+
this.TestLogWithArguments(Level.Trace, typeof(CtorPropertyLoggerClass).GetMethod("LogTraceWithArguments"));
64+
}
65+
66+
/// <summary>
67+
/// Tests trace exception logging
68+
/// </summary>
69+
[Fact]
70+
public void LogTraceException()
71+
{
72+
this.TestLogException(Level.Trace, typeof(CtorPropertyLoggerClass).GetMethod("LogTraceException"));
73+
}
74+
75+
/// <summary>
76+
/// Tests trace exception logging with arguments
77+
/// </summary>
78+
[Fact]
79+
public void LogTraceWithException()
80+
{
81+
this.TestLogWithException(Level.Trace, typeof(CtorPropertyLoggerClass).GetMethod("LogTraceWithException"));
82+
}
83+
4784
/// <summary>
4885
/// Tests info logging
4986
/// </summary>
@@ -54,7 +91,25 @@ public void LogInfo()
5491
}
5592

5693
/// <summary>
57-
/// Tests info logging with exception
94+
/// Tests info logging with arguments
95+
/// </summary>
96+
[Fact]
97+
public void LogInfoWithArguments()
98+
{
99+
this.TestLogWithArguments(Level.Info, typeof(CtorPropertyLoggerClass).GetMethod("LogInfoWithArguments"));
100+
}
101+
102+
/// <summary>
103+
/// Tests info exception logging
104+
/// </summary>
105+
[Fact]
106+
public void LogInfoException()
107+
{
108+
this.TestLogException(Level.Info, typeof(CtorPropertyLoggerClass).GetMethod("LogInfoException"));
109+
}
110+
111+
/// <summary>
112+
/// Tests info exception logging with arguments
58113
/// </summary>
59114
[Fact]
60115
public void LogInfoWithException()
@@ -72,7 +127,25 @@ public void LogDebug()
72127
}
73128

74129
/// <summary>
75-
/// Tests debug logging with exception
130+
/// Tests debug logging with arguments
131+
/// </summary>
132+
[Fact]
133+
public void LogDebugWithArguments()
134+
{
135+
this.TestLogWithArguments(Level.Debug, typeof(CtorPropertyLoggerClass).GetMethod("LogDebugWithArguments"));
136+
}
137+
138+
/// <summary>
139+
/// Tests debug exception logging
140+
/// </summary>
141+
[Fact]
142+
public void LogDebugException()
143+
{
144+
this.TestLogException(Level.Debug, typeof(CtorPropertyLoggerClass).GetMethod("LogDebugException"));
145+
}
146+
147+
/// <summary>
148+
/// Tests debug exception logging with arguments
76149
/// </summary>
77150
[Fact]
78151
public void LogDebugWithException()
@@ -90,7 +163,25 @@ public void LogWarn()
90163
}
91164

92165
/// <summary>
93-
/// Tests warn logging with exception
166+
/// Tests warn logging with arguments
167+
/// </summary>
168+
[Fact]
169+
public void LogWarnWithArguments()
170+
{
171+
this.TestLogWithArguments(Level.Warn, typeof(CtorPropertyLoggerClass).GetMethod("LogWarnWithArguments"));
172+
}
173+
174+
/// <summary>
175+
/// Tests warn exception logging
176+
/// </summary>Exception
177+
[Fact]
178+
public void LogWarnException()
179+
{
180+
this.TestLogException(Level.Warn, typeof(CtorPropertyLoggerClass).GetMethod("LogWarnException"));
181+
}
182+
183+
/// <summary>
184+
/// Tests warn exception logging with arguments
94185
/// </summary>
95186
[Fact]
96187
public void LogWarnWithException()
@@ -108,7 +199,25 @@ public void LogError()
108199
}
109200

110201
/// <summary>
111-
/// Tests error logging with exception
202+
/// Tests error logging with arguments
203+
/// </summary>
204+
[Fact]
205+
public void LogErrorWithArguments()
206+
{
207+
this.TestLogWithArguments(Level.Error, typeof(CtorPropertyLoggerClass).GetMethod("LogErrorWithArguments"));
208+
}
209+
210+
/// <summary>
211+
/// Tests error exception logging
212+
/// </summary>
213+
[Fact]
214+
public void LogErrorException()
215+
{
216+
this.TestLogException(Level.Error, typeof(CtorPropertyLoggerClass).GetMethod("LogErrorException"));
217+
}
218+
219+
/// <summary>
220+
/// Tests error exception logging with arguments
112221
/// </summary>
113222
[Fact]
114223
public void LogErrorWithException()
@@ -126,7 +235,25 @@ public void LogFatal()
126235
}
127236

128237
/// <summary>
129-
/// Tests fatal logging with exception
238+
/// Tests fatal logging with arguments
239+
/// </summary>
240+
[Fact]
241+
public void LogFatalWithArguments()
242+
{
243+
this.TestLogWithArguments(Level.Fatal, typeof(CtorPropertyLoggerClass).GetMethod("LogFatalWithArguments"));
244+
}
245+
246+
/// <summary>
247+
/// Tests fatal exception logging
248+
/// </summary>
249+
[Fact]
250+
public void LogFatalException()
251+
{
252+
this.TestLogException(Level.Fatal, typeof(CtorPropertyLoggerClass).GetMethod("LogFatalException"));
253+
}
254+
255+
/// <summary>
256+
/// Tests fatal exception logging with arguments
130257
/// </summary>
131258
[Fact]
132259
public void LogFatalWithException()
@@ -193,6 +320,29 @@ private void TestLog(Level level, MethodInfo methodInfo)
193320
{
194321
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
195322

323+
methodInfo.Invoke(loggerClass, new object[] { "test message" });
324+
325+
var events = testAppender.GetEvents();
326+
var lastLogEvent = testAppender.GetEvents()[testAppender.GetEvents().Length - 1];
327+
lastLogEvent.Should().NotBeNull();
328+
lastLogEvent.RenderedMessage.Should().Be("test message");
329+
lastLogEvent.Level.Should().Be(level);
330+
lastLogEvent.LoggerName.Should().Be(loggerClass.GetType().FullName);
331+
lastLogEvent.ExceptionObject.Should().BeNull();
332+
}
333+
}
334+
335+
/// <summary>
336+
/// Tests the logging without exception.
337+
/// </summary>
338+
/// <param name="level">The log level to be tested.</param>
339+
/// <param name="methodInfo">The method info to be called to invoke the method to be tested.</param>
340+
private void TestLogWithArguments(Level level, MethodInfo methodInfo)
341+
{
342+
using (var kernel = this.CreateKernel())
343+
{
344+
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
345+
196346
methodInfo.Invoke(loggerClass, new object[] { "message {0}", new object[] { 1 } });
197347

198348
var lastLogEvent = testAppender.GetEvents()[testAppender.GetEvents().Length - 1];
@@ -209,6 +359,29 @@ private void TestLog(Level level, MethodInfo methodInfo)
209359
/// </summary>
210360
/// <param name="logLevel">The log level to be tested.</param>
211361
/// <param name="methodInfo">The method info to be called to invoke the method to be tested.</param>
362+
private void TestLogException(Level logLevel, MethodInfo methodInfo)
363+
{
364+
using (var kernel = this.CreateKernel())
365+
{
366+
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
367+
var exception = new ArgumentException();
368+
369+
methodInfo.Invoke(loggerClass, new object[] { "exception message", exception });
370+
371+
var lastLogEvent = testAppender.GetEvents()[testAppender.GetEvents().Length - 1];
372+
lastLogEvent.Should().NotBeNull();
373+
lastLogEvent.RenderedMessage.Should().Be("exception message");
374+
lastLogEvent.Level.Should().Be(logLevel);
375+
lastLogEvent.LoggerName.Should().Be(loggerClass.GetType().FullName);
376+
lastLogEvent.ExceptionObject.Should().BeSameAs(exception);
377+
}
378+
}
379+
380+
/// <summary>
381+
/// Tests the logging with exception.
382+
/// </summary>
383+
/// <param name="level">The log level to be tested.</param>
384+
/// <param name="methodInfo">The method info to be called to invoke the method to be tested.</param>
212385
private void TestLogWithException(Level level, MethodInfo methodInfo)
213386
{
214387
using (var kernel = this.CreateKernel())
@@ -225,7 +398,6 @@ private void TestLogWithException(Level level, MethodInfo methodInfo)
225398
lastLogEvent.LoggerName.Should().Be(loggerClass.GetType().FullName);
226399
lastLogEvent.ExceptionObject.Should().BeSameAs(exception);
227400
}
228-
229401
}
230402
}
231403

0 commit comments

Comments
 (0)