Unit tests, fix for logname override functionality #4

Merged
merged 2 commits into from Apr 18, 2012
@@ -75,13 +75,13 @@ public object Resolve(CreationContext context, ISubDependencyResolver parentReso
{
return string.IsNullOrEmpty(logName)
? extendedLoggerFactory.Create(model.Implementation)
- : extendedLoggerFactory.Create(logName).CreateChildLogger(model.Implementation.Name);
+ : extendedLoggerFactory.Create(logName).CreateChildLogger(model.Implementation.FullName);
}
Debug.Assert(loggerFactory != null);
return string.IsNullOrEmpty(logName)
? loggerFactory.Create(model.Implementation)
- : loggerFactory.Create(logName).CreateChildLogger(model.Implementation.Name);
+ : loggerFactory.Create(logName).CreateChildLogger(model.Implementation.FullName);
}
}
}
@@ -574,9 +574,11 @@
<Compile Include="LoggingFacility\ExtendedLog4NetFacilityTestCase.cs" />
<Compile Include="LoggingFacility\ExtendedNLogFacilityTests.cs" />
<Compile Include="LoggingFacility\Facilities77.cs" />
+ <Compile Include="LoggingFacility\Log4NetFacilityLognameOverrideTests.cs" />
<Compile Include="LoggingFacility\Log4NetFacilityTests.cs" />
<Compile Include="LoggingFacility\NLogFacilityTests.cs" />
<Compile Include="LoggingFacility\NullFacilityTest.cs" />
+ <Compile Include="LoggingFacility\OverrideLoggerTest.cs" />
<Compile Include="LoggingFacility\TraceFacilityTest.cs" />
<Compile Include="IsDefaultTestCase.cs" />
<Compile Include="MicroKernel\NotImplementedDependencyResolver.cs" />
@@ -0,0 +1,51 @@
+namespace Castle.Facilities.Logging.Tests
+{
+ using System;
+ using System.IO;
+
+ using Castle.Facilities.Logging.Tests.Classes;
+ using Castle.MicroKernel.Registration;
+ using Castle.Windsor;
+
+ using log4net;
+ using log4net.Appender;
+ using log4net.Layout;
+ using log4net.Repository.Hierarchy;
+
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class Log4NetFacilityLognameOverrideTests : OverrideLoggerTest
+ {
+ private IWindsorContainer container;
+
+ [SetUp]
+ public void Setup()
+ {
+ container = base.CreateConfiguredContainer(LoggerImplementation.ExtendedLog4net, string.Empty, "Override");
+ }
+
+ [TearDown]
+ public void Teardown()
+ {
+ container.Dispose();
+ }
+
+ [Test]
+ public void OverrideTest()
+ {
+ container.Register(Component.For(typeof(SimpleLoggingComponent)).Named("component"));
+ SimpleLoggingComponent test = container.Resolve<SimpleLoggingComponent>("component");
+
+ test.DoSomething();
+
+ String expectedLogOutput = String.Format("[INFO ] [Override.{0}] - Hello world" + Environment.NewLine, typeof(SimpleLoggingComponent).FullName);
+ MemoryAppender memoryAppender = ((Hierarchy)LogManager.GetRepository()).Root.GetAppender("memory") as MemoryAppender;
+ TextWriter actualLogOutput = new StringWriter();
+ PatternLayout patternLayout = new PatternLayout("[%-5level] [%logger] - %message%newline");
+ patternLayout.Format(actualLogOutput, memoryAppender.GetEvents()[0]);
+
+ Assert.AreEqual(expectedLogOutput, actualLogOutput.ToString());
+ }
+ }
+}
@@ -63,5 +63,6 @@ public void SimpleTest()
Assert.AreEqual(expectedLogOutput, actualLogOutput.ToString());
}
}
+
#endif
}
@@ -0,0 +1,21 @@
+namespace Castle.Facilities.Logging.Tests
+{
+ using System;
+
+ using Castle.MicroKernel.SubSystems.Configuration;
+ using Castle.Windsor;
+
+ public abstract class OverrideLoggerTest : BaseTest
+ {
+ protected virtual IWindsorContainer CreateConfiguredContainer(LoggerImplementation loggerApi, String custom, string logName)
+ {
+ IWindsorContainer container = new WindsorContainer(new DefaultConfigurationStore());
+ var configFile = GetConfigFile(loggerApi);
+
+ container.AddFacility<LoggingFacility>(f => f.LogUsing(loggerApi).WithConfig(configFile).ToLog(logName));
+
+ return container;
+ }
+
+ }
+}