Skip to content

Commit 76571db

Browse files
committed
Merge pull request #29 from daub815/useCorrectWrappedLogger
Properly wrapped Log4NetLogger using preferred method
2 parents 2313ced + da1e5a3 commit 76571db

File tree

3 files changed

+276
-40
lines changed

3 files changed

+276
-40
lines changed

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

Lines changed: 224 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,232 @@
11
namespace Ninject.Extensions.Logging.Log4Net
22
{
3-
using Ninject;
4-
using Ninject.Extensions.Logging.Log4Net.Infrastructure;
5-
using Ninject.Modules;
3+
using System;
4+
using System.Reflection;
5+
6+
using FluentAssertions;
7+
8+
using log4net.Appender;
9+
using log4net.Core;
10+
using log4net.Repository.Hierarchy;
11+
12+
using Ninject;
13+
using Ninject.Extensions.Logging.Classes;
14+
using Ninject.Extensions.Logging.Log4Net.Infrastructure;
15+
using Ninject.Modules;
16+
17+
using Xunit;
18+
619

720
public class Log4netTests : Log4netTestingContext
821
{
9-
// todo, add log4net specific tests
22+
/// <summary>
23+
/// the appender that is used to intercept the logging events
24+
/// </summary>
25+
private static readonly log4net.Appender.MemoryAppender testAppender;
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="Log4netTests"/> class.
29+
/// </summary>
30+
static Log4netTests()
31+
{
32+
// Create the appender
33+
testAppender = new MemoryAppender
34+
{
35+
Name = "Unit Test Appender",
36+
Layout = new log4net.Layout.PatternLayout("%message"),
37+
Threshold = Level.Trace
38+
};
39+
testAppender.ActivateOptions();
40+
41+
// Configure the default repository
42+
var root = ((Hierarchy)log4net.LogManager.GetRepository()).Root;
43+
root.AddAppender(testAppender);
44+
root.Repository.Configured = true;
45+
}
46+
47+
/// <summary>
48+
/// Tests info logging
49+
/// </summary>
50+
[Fact]
51+
public void LogInfo()
52+
{
53+
this.TestLog(Level.Info, typeof(CtorPropertyLoggerClass).GetMethod("LogInfo"));
54+
}
55+
56+
/// <summary>
57+
/// Tests info logging with exception
58+
/// </summary>
59+
[Fact]
60+
public void LogInfoWithException()
61+
{
62+
this.TestLogWithException(Level.Info, typeof(CtorPropertyLoggerClass).GetMethod("LogInfoWithException"));
63+
}
64+
65+
/// <summary>
66+
/// Tests debug logging
67+
/// </summary>
68+
[Fact]
69+
public void LogDebug()
70+
{
71+
this.TestLog(Level.Debug, typeof(CtorPropertyLoggerClass).GetMethod("LogDebug"));
72+
}
73+
74+
/// <summary>
75+
/// Tests debug logging with exception
76+
/// </summary>
77+
[Fact]
78+
public void LogDebugWithException()
79+
{
80+
this.TestLogWithException(Level.Debug, typeof(CtorPropertyLoggerClass).GetMethod("LogDebugWithException"));
81+
}
82+
83+
/// <summary>
84+
/// Tests warn logging
85+
/// </summary>
86+
[Fact]
87+
public void LogWarn()
88+
{
89+
this.TestLog(Level.Warn, typeof(CtorPropertyLoggerClass).GetMethod("LogWarn"));
90+
}
91+
92+
/// <summary>
93+
/// Tests warn logging with exception
94+
/// </summary>
95+
[Fact]
96+
public void LogWarnWithException()
97+
{
98+
this.TestLogWithException(Level.Warn, typeof(CtorPropertyLoggerClass).GetMethod("LogWarnWithException"));
99+
}
100+
101+
/// <summary>
102+
/// Tests error logging
103+
/// </summary>
104+
[Fact]
105+
public void LogError()
106+
{
107+
this.TestLog(Level.Error, typeof(CtorPropertyLoggerClass).GetMethod("LogError"));
108+
}
109+
110+
/// <summary>
111+
/// Tests error logging with exception
112+
/// </summary>
113+
[Fact]
114+
public void LogErrorWithException()
115+
{
116+
this.TestLogWithException(Level.Error, typeof(CtorPropertyLoggerClass).GetMethod("LogErrorWithException"));
117+
}
118+
119+
/// <summary>
120+
/// Tests fatal logging
121+
/// </summary>
122+
[Fact]
123+
public void LogFatal()
124+
{
125+
this.TestLog(Level.Fatal, typeof(CtorPropertyLoggerClass).GetMethod("LogFatal"));
126+
}
127+
128+
/// <summary>
129+
/// Tests fatal logging with exception
130+
/// </summary>
131+
[Fact]
132+
public void LogFatalWithException()
133+
{
134+
this.TestLogWithException(Level.Fatal, typeof(CtorPropertyLoggerClass).GetMethod("LogFatalWithException"));
135+
}
136+
137+
#if WINDOWS_PHONE
138+
[Fact]
139+
public void PublicLoggerPropertyIsInjected()
140+
{
141+
using (var kernel = this.CreateKernel())
142+
{
143+
var loggerClass = kernel.Get<PublicPropertyLoggerClass>();
144+
loggerClass.Logger.Should().NotBeNull();
145+
loggerClass.Logger.Type.Should().Be(typeof(PublicPropertyLoggerClass));
146+
loggerClass.Logger.GetType().Should().Be(this.LoggerType);
147+
}
148+
}
149+
150+
[Fact]
151+
public void NonPublicLoggerPropertyIsNotInjected()
152+
{
153+
using (var kernel = this.CreateKernel())
154+
{
155+
var loggerClass = kernel.Get<NonPublicPropertyLoggerClass>();
156+
loggerClass.Logger.Should().BeNull();
157+
}
158+
}
159+
160+
[Fact]
161+
public virtual void CtorLoggerPropertyIsInjected()
162+
{
163+
using (var kernel = this.CreateKernel())
164+
{
165+
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
166+
loggerClass.Logger.Should().NotBeNull();
167+
loggerClass.Logger.Type.Should().Be(typeof(CtorPropertyLoggerClass));
168+
loggerClass.Logger.GetType().Should().Be(this.LoggerType);
169+
}
170+
}
171+
172+
[Fact]
173+
public void ObjectCanGetsItsOwnLogger()
174+
{
175+
using (var kernel = this.CreateKernel())
176+
{
177+
var loggerClass = kernel.Get<ObjectGetsItsOwnLogger>();
178+
loggerClass.Logger.Should().NotBeNull();
179+
loggerClass.Logger.Type.Should().Be(typeof(ObjectGetsItsOwnLogger));
180+
loggerClass.Logger.GetType().Should().Be(this.LoggerType);
181+
}
182+
}
183+
#endif
184+
185+
/// <summary>
186+
/// Tests the logging without exception.
187+
/// </summary>
188+
/// <param name="level">The log level to be tested.</param>
189+
/// <param name="methodInfo">The method info to be called to invoke the method to be tested.</param>
190+
private void TestLog(Level level, MethodInfo methodInfo)
191+
{
192+
using (var kernel = this.CreateKernel())
193+
{
194+
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
195+
196+
methodInfo.Invoke(loggerClass, new object[] { "message {0}", new object[] { 1 } });
197+
198+
var lastLogEvent = testAppender.GetEvents()[testAppender.GetEvents().Length - 1];
199+
lastLogEvent.Should().NotBeNull();
200+
lastLogEvent.RenderedMessage.Should().Be("message 1");
201+
lastLogEvent.Level.Should().Be(level);
202+
lastLogEvent.LoggerName.Should().Be(loggerClass.GetType().FullName);
203+
lastLogEvent.ExceptionObject.Should().BeNull();
204+
}
205+
}
206+
207+
/// <summary>
208+
/// Tests the logging with exception.
209+
/// </summary>
210+
/// <param name="logLevel">The log level to be tested.</param>
211+
/// <param name="methodInfo">The method info to be called to invoke the method to be tested.</param>
212+
private void TestLogWithException(Level level, MethodInfo methodInfo)
213+
{
214+
using (var kernel = this.CreateKernel())
215+
{
216+
var loggerClass = kernel.Get<CtorPropertyLoggerClass>();
217+
var exception = new ArgumentException();
218+
219+
methodInfo.Invoke(loggerClass, new object[] { exception, "message {0}", new object[] { 1 } });
220+
221+
var lastLogEvent = testAppender.GetEvents()[testAppender.GetEvents().Length - 1];
222+
lastLogEvent.Should().NotBeNull();
223+
lastLogEvent.RenderedMessage.Should().Be("message 1");
224+
lastLogEvent.Level.Should().Be(level);
225+
lastLogEvent.LoggerName.Should().Be(loggerClass.GetType().FullName);
226+
lastLogEvent.ExceptionObject.Should().BeSameAs(exception);
227+
}
228+
229+
}
10230
}
11231

12232
#if !SILVERLIGHT && !NETCF

src/Ninject.Extensions.Logging.Log4Net.Test/Ninject.Extensions.Logging.Log4Net.Test.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
<ErrorReport>prompt</ErrorReport>
3535
<WarningLevel>4</WarningLevel>
3636
</PropertyGroup>
37-
<PropertyGroup>
38-
<StartupObject />
39-
</PropertyGroup>
4037
<ItemGroup>
4138
<Reference Include="log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
4239
<HintPath>..\..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath>
@@ -51,6 +48,12 @@
5148
<Reference Include="System.Data.DataSetExtensions" />
5249
<Reference Include="System.Data" />
5350
<Reference Include="System.Xml" />
51+
<Reference Include="FluentAssertions">
52+
<HintPath>..\..\tools\FluentAssertions\Net-3.5\FluentAssertions.dll</HintPath>
53+
</Reference>
54+
<Reference Include="xunit">
55+
<HintPath>..\..\tools\xunit.net\xunit.dll</HintPath>
56+
</Reference>
5457
</ItemGroup>
5558
<ItemGroup>
5659
<Compile Include="Infrastructure\Log4netTestingContext.cs" />

0 commit comments

Comments
 (0)