From 9026e7d7cda0296aafe744d0db0f407236571ee3 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Tue, 1 Oct 2019 20:55:50 +0300 Subject: [PATCH] Configure log4net from embedded resource log4net.xml --- src/NHibernate.Test/App.config | 46 +------------------ src/NHibernate.Test/Hql/Ast/ParsingFixture.cs | 4 +- src/NHibernate.Test/Log4netConfiguration.cs | 2 - .../NHSpecificTest/NH1587/Fixture.cs | 3 -- src/NHibernate.Test/NHibernate.Test.csproj | 1 + src/NHibernate.Test/TestCase.cs | 7 --- src/NHibernate.Test/TestsContext.cs | 38 +++++---------- .../TypesTest/TypeFactoryFixture.cs | 6 --- src/NHibernate.Test/log4net.xml | 45 ++++++++++++++++++ 9 files changed, 59 insertions(+), 93 deletions(-) delete mode 100644 src/NHibernate.Test/Log4netConfiguration.cs create mode 100644 src/NHibernate.Test/log4net.xml diff --git a/src/NHibernate.Test/App.config b/src/NHibernate.Test/App.config index d3965012af5..40cf748a495 100644 --- a/src/NHibernate.Test/App.config +++ b/src/NHibernate.Test/App.config @@ -3,7 +3,6 @@
-
@@ -52,50 +51,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/NHibernate.Test/Hql/Ast/ParsingFixture.cs b/src/NHibernate.Test/Hql/Ast/ParsingFixture.cs index 040ba037b9a..45be4789f1c 100644 --- a/src/NHibernate.Test/Hql/Ast/ParsingFixture.cs +++ b/src/NHibernate.Test/Hql/Ast/ParsingFixture.cs @@ -74,8 +74,6 @@ namespace NHibernate.Test.Hql.Ast // [Test] // public void BasicQuery() // { - // XmlConfigurator.Configure(); - // string input = "select o.id, li.id from NHibernate.Test.CompositeId.Order o join o.LineItems li";// join o.LineItems li"; // ISessionFactoryImplementor sfi = SetupSFI(); @@ -175,4 +173,4 @@ namespace NHibernate.Test.Hql.Ast // } // } //} -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/Log4netConfiguration.cs b/src/NHibernate.Test/Log4netConfiguration.cs deleted file mode 100644 index 841ecc09911..00000000000 --- a/src/NHibernate.Test/Log4netConfiguration.cs +++ /dev/null @@ -1,2 +0,0 @@ -using log4net.Config; -[assembly: XmlConfigurator()] \ No newline at end of file diff --git a/src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs index 6a716ce5a66..68eb38d6bab 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs @@ -1,5 +1,3 @@ -using log4net; -using log4net.Config; using log4net.Core; using NHibernate.Cfg; using NUnit.Framework; @@ -12,7 +10,6 @@ public class Fixture [Test] public void Bug() { - XmlConfigurator.Configure(LogManager.GetRepository(typeof(Fixture).Assembly)); var cfg = new Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); diff --git a/src/NHibernate.Test/NHibernate.Test.csproj b/src/NHibernate.Test/NHibernate.Test.csproj index c88f2913de6..a0dd6cf10bd 100644 --- a/src/NHibernate.Test/NHibernate.Test.csproj +++ b/src/NHibernate.Test/NHibernate.Test.csproj @@ -25,6 +25,7 @@ + Always diff --git a/src/NHibernate.Test/TestCase.cs b/src/NHibernate.Test/TestCase.cs index 4c9ea610c6d..98f8043cc57 100644 --- a/src/NHibernate.Test/TestCase.cs +++ b/src/NHibernate.Test/TestCase.cs @@ -4,7 +4,6 @@ using System.Data; using System.Reflection; using log4net; -using log4net.Config; using NHibernate.Cfg; using NHibernate.Connection; using NHibernate.Engine; @@ -53,12 +52,6 @@ protected virtual string MappingsAssembly protected SchemaExport SchemaExport => _schemaExport ?? (_schemaExport = new SchemaExport(cfg)); - static TestCase() - { - // Configure log4net here since configuration through an attribute doesn't always work. - XmlConfigurator.Configure(LogManager.GetRepository(typeof(TestCase).Assembly)); - } - /// /// Creates the tables used in this TestCase /// diff --git a/src/NHibernate.Test/TestsContext.cs b/src/NHibernate.Test/TestsContext.cs index 4eae9d561e5..e80cca726aa 100644 --- a/src/NHibernate.Test/TestsContext.cs +++ b/src/NHibernate.Test/TestsContext.cs @@ -1,9 +1,8 @@ -#if NETCOREAPP2_0 -using NUnit.Framework; - +using NUnit.Framework; using System.Configuration; -using System.IO; -using log4net.Repository.Hierarchy; +using System.Reflection; +using log4net; +using log4net.Config; using NHibernate.Cfg; namespace NHibernate.Test @@ -11,40 +10,25 @@ namespace NHibernate.Test [SetUpFixture] public class TestsContext { - private static bool ExecutingWithVsTest { get; } = - System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name == "testhost"; + private static readonly Assembly TestAssembly = typeof(TestsContext).Assembly; [OneTimeSetUp] public void RunBeforeAnyTests() { + ConfigureLog4Net(); + //When .NET Core App 2.0 tests run from VS/VSTest the entry assembly is "testhost.dll" //so we need to explicitly load the configuration - if (ExecutingWithVsTest) + if (Assembly.GetEntryAssembly() != null) { - var assemblyPath = Path.Combine(TestContext.CurrentContext.TestDirectory, Path.GetFileName(typeof(TestsContext).Assembly.Location)); - ConfigurationProvider.Current = new SystemConfigurationProvider(ConfigurationManager.OpenExeConfiguration(assemblyPath)); + ConfigurationProvider.Current = new SystemConfigurationProvider(ConfigurationManager.OpenExeConfiguration(TestAssembly.Location)); } - - ConfigureLog4Net(); } private static void ConfigureLog4Net() { - var hierarchy = (Hierarchy)log4net.LogManager.GetRepository(typeof(TestsContext).Assembly); - - var consoleAppender = new log4net.Appender.ConsoleAppender - { - Layout = new log4net.Layout.PatternLayout("%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"), - }; - - ((Logger)hierarchy.GetLogger("NHibernate.Hql.Ast.ANTLR")).Level = log4net.Core.Level.Off; - ((Logger)hierarchy.GetLogger("NHibernate.SQL")).Level = log4net.Core.Level.Off; - ((Logger)hierarchy.GetLogger("NHibernate.AdoNet.AbstractBatcher")).Level = log4net.Core.Level.Off; - ((Logger)hierarchy.GetLogger("NHibernate.Tool.hbm2ddl.SchemaExport")).Level = log4net.Core.Level.Error; - hierarchy.Root.Level = log4net.Core.Level.Warn; - hierarchy.Root.AddAppender(consoleAppender); - hierarchy.Configured = true; + using (var log4NetXml = TestAssembly.GetManifestResourceStream("NHibernate.Test.log4net.xml")) + XmlConfigurator.Configure(LogManager.GetRepository(TestAssembly), log4NetXml); } } } -#endif diff --git a/src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs b/src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs index b7b870d3f43..4d01a52e9be 100644 --- a/src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs +++ b/src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs @@ -1,6 +1,5 @@ using System; using log4net; -using log4net.Repository.Hierarchy; using NHibernate.Type; using NUnit.Framework; @@ -12,11 +11,6 @@ namespace NHibernate.Test.TypesTest [TestFixture] public class TypeFactoryFixture { - public TypeFactoryFixture() - { - log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(typeof(TypeFactoryFixture).Assembly)); - } - private static readonly ILog log = LogManager.GetLogger(typeof(TypeFactoryFixture)); /// diff --git a/src/NHibernate.Test/log4net.xml b/src/NHibernate.Test/log4net.xml new file mode 100644 index 00000000000..01844a5d696 --- /dev/null +++ b/src/NHibernate.Test/log4net.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +