Skip to content

NH-4019 - Pass assembly into log4net functions #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NHibernate.Test/FilterTest/ConfigFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void FilterDefWithoutReference()
var cfg = GetConfiguration();
cfg.AddXmlString(filterDef);
var memoryAppender = new MemoryAppender();
BasicConfigurator.Configure(memoryAppender);
BasicConfigurator.Configure(LogManager.GetRepository(typeof(ConfigFixture).Assembly), memoryAppender);
try
{
cfg.BuildSessionFactory();
Expand Down
12 changes: 6 additions & 6 deletions src/NHibernate.Test/IdTest/AssignedFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void OnTearDown()
[Test]
public void SaveOrUpdate_Save()
{
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand All @@ -70,7 +70,7 @@ public void SaveOrUpdate_Save()
[Test]
public void SaveNoWarning()
{
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand Down Expand Up @@ -104,7 +104,7 @@ public void SaveOrUpdate_Update()
t.Commit();
}

using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand Down Expand Up @@ -142,7 +142,7 @@ public void UpdateNoWarning()
t.Commit();
}

using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand Down Expand Up @@ -179,7 +179,7 @@ public void InsertCascade()
t.Commit();
}

using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand Down Expand Up @@ -218,7 +218,7 @@ public void InsertCascadeNoWarning()
t.Commit();
}

using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
using (ISession s = OpenSession())
{
ITransaction t = s.BeginTransaction();
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate.Test/LogSpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public LogSpy(ILog log) : this(log, false) { }
public LogSpy(System.Type loggerType) : this(LogManager.GetLogger(loggerType), false) { }
public LogSpy(System.Type loggerType, bool disable) : this(LogManager.GetLogger(loggerType), disable) { }

public LogSpy(string loggerName) : this(LogManager.GetLogger(loggerName), false) { }
public LogSpy(string loggerName, bool disable) : this(LogManager.GetLogger(loggerName), disable) { }
public LogSpy(string loggerName) : this(LogManager.GetLogger(typeof(LogSpy).Assembly, loggerName), false) { }
public LogSpy(string loggerName, bool disable) : this(LogManager.GetLogger(typeof(LogSpy).Assembly, loggerName), disable) { }

public MemoryAppender Appender
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/Logs/LogsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public TextLogSpy(string loggerName, string pattern)
Threshold = Level.All,
Writer = new StringWriter(stringBuilder)
};
loggerImpl = (Logger)LogManager.GetLogger(loggerName).Logger;
loggerImpl = (Logger)LogManager.GetLogger(typeof(LogsFixture).Assembly, loggerName).Logger;
loggerImpl.AddAppender(appender);
loggerImpl.Level = Level.All;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH1093/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void NormalList()
protected override void BuildSessionFactory()
{
// Without configured cache, should log warn.
using (var ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
using (var ls = new LogSpy(LogManager.GetLogger(typeof(Fixture).Assembly, "NHibernate"), Level.Warn))
{
base.BuildSessionFactory();
Assert.That(ls.GetWholeLog(), Does.Contain("Fake cache used"));
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using log4net;
using log4net.Config;
using log4net.Core;
using NHibernate.Cfg;
Expand All @@ -11,7 +12,7 @@ public class Fixture
[Test]
public void Bug()
{
XmlConfigurator.Configure();
XmlConfigurator.Configure(LogManager.GetRepository(typeof(Fixture).Assembly));
var cfg = new Configuration();
if (TestConfigurationHelper.hibernateConfigFile != null)
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2386/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)

protected override void OnTearDown() {
if (memoryAppender != null) {
var repository = (Hierarchy) LogManager.GetRepository();
var repository = (Hierarchy) LogManager.GetRepository(typeof(Test).Assembly);
repository.Root.RemoveAppender(memoryAppender);
memoryAppender = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2779/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Test()

using (var session = OpenSession())
using (var tx = session.BeginTransaction())
using (new LogSpy(LogManager.GetLogger("NHibernate"), Level.All)) // <-- Logging must be set DEBUG to reproduce bug
using (new LogSpy(LogManager.GetLogger(typeof(Fixture).Assembly, "NHibernate"), Level.All)) // <-- Logging must be set DEBUG to reproduce bug
{
Order order = session.Get<Order>("Order-1");
Assert.IsNotNull(order);
Expand Down
4 changes: 3 additions & 1 deletion src/NHibernate.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
using NUnit.Framework;
using NHibernate.Hql.Ast.ANTLR;
using System.Collections.Concurrent;
using System.IO;
using NUnit.Framework.Interfaces;
using System.Text;
using log4net.Util;
using static NUnit.Framework.TestContext;

namespace NHibernate.Test
Expand Down Expand Up @@ -68,7 +70,7 @@ protected virtual string MappingsAssembly
static TestCase()
{
// Configure log4net here since configuration through an attribute doesn't always work.
XmlConfigurator.Configure();
XmlConfigurator.Configure(LogManager.GetRepository(typeof(TestCase).Assembly));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TypeFactoryFixture
{
public TypeFactoryFixture()
{
log4net.Config.XmlConfigurator.Configure();
log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(typeof(TypeFactoryFixture).Assembly));
}

private static readonly ILog log = LogManager.GetLogger(typeof(TypeFactoryFixture));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ThreadSafeDictionaryFixture
{
public ThreadSafeDictionaryFixture()
{
log4net.Config.XmlConfigurator.Configure();
log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(typeof(ThreadSafeDictionaryFixture).Assembly));
}

private static readonly ILog log = LogManager.GetLogger(typeof(ThreadSafeDictionaryFixture));
Expand Down
16 changes: 13 additions & 3 deletions src/NHibernate/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;

namespace NHibernate
Expand Down Expand Up @@ -222,16 +223,16 @@ public void WarnFormat(string format, params object[] args)
public class Log4NetLoggerFactory: ILoggerFactory
{
private static readonly System.Type LogManagerType = System.Type.GetType("log4net.LogManager, log4net");
private static readonly Func<string, object> GetLoggerByNameDelegate;
private static readonly Func<Assembly, string, object> GetLoggerByNameDelegate;
private static readonly Func<System.Type, object> GetLoggerByTypeDelegate;
static Log4NetLoggerFactory()
{
GetLoggerByNameDelegate = GetGetLoggerMethodCall<string>();
GetLoggerByNameDelegate = GetGetLoggerByNameMethodCall();
GetLoggerByTypeDelegate = GetGetLoggerMethodCall<System.Type>();
}
public IInternalLogger LoggerFor(string keyName)
{
return new Log4NetLogger(GetLoggerByNameDelegate(keyName));
return new Log4NetLogger(GetLoggerByNameDelegate(typeof(Log4NetLoggerFactory).Assembly, keyName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need an overload for specifying another Assembly than NHibernate.dll? They are two calls in NHibernate.Test.dll which were likely resolving to the test assembly instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the delegate is running in the context of the Log4NetLoggerFactory class, so when Assembly.GetCallingAssembly() was called in log4net, it only goes one call-level up--back to NHibernate.

}

public IInternalLogger LoggerFor(System.Type type)
Expand All @@ -247,6 +248,15 @@ private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
MethodCallExpression methodCall = Expression.Call(null, method, resultValue = keyParam);
return Expression.Lambda<Func<TParameter, object>>(methodCall, resultValue).Compile();
}

private static Func<Assembly, string, object> GetGetLoggerByNameMethodCall()
{
var method = LogManagerType.GetMethod("GetLogger", new[] {typeof(Assembly), typeof(string)});
ParameterExpression nameParam = Expression.Parameter(typeof(string), "name");
ParameterExpression repositoryAssemblyParam = Expression.Parameter(typeof(Assembly), "repositoryAssembly");
MethodCallExpression methodCall = Expression.Call(null, method, repositoryAssemblyParam, nameParam);
return Expression.Lambda<Func<Assembly, string, object>>(methodCall, repositoryAssemblyParam, nameParam).Compile();
}
}

public class Log4NetLogger: IInternalLogger
Expand Down