Skip to content

Commit be69f1b

Browse files
committed
NH-4019 - Pass assembly into log4net functions because it does not have access to Assembly.GetCallingAssembly() in .netstandard version.
1 parent 10a0d59 commit be69f1b

File tree

12 files changed

+33
-20
lines changed

12 files changed

+33
-20
lines changed

src/NHibernate.Test/FilterTest/ConfigFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public void FilterDefWithoutReference()
242242
var cfg = GetConfiguration();
243243
cfg.AddXmlString(filterDef);
244244
var memoryAppender = new MemoryAppender();
245-
BasicConfigurator.Configure(memoryAppender);
245+
BasicConfigurator.Configure(LogManager.GetRepository(typeof(ConfigFixture).Assembly), memoryAppender);
246246
try
247247
{
248248
cfg.BuildSessionFactory();

src/NHibernate.Test/IdTest/AssignedFixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override void OnTearDown()
4343
[Test]
4444
public void SaveOrUpdate_Save()
4545
{
46-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
46+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
4747
using (ISession s = OpenSession())
4848
{
4949
ITransaction t = s.BeginTransaction();
@@ -70,7 +70,7 @@ public void SaveOrUpdate_Save()
7070
[Test]
7171
public void SaveNoWarning()
7272
{
73-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
73+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
7474
using (ISession s = OpenSession())
7575
{
7676
ITransaction t = s.BeginTransaction();
@@ -104,7 +104,7 @@ public void SaveOrUpdate_Update()
104104
t.Commit();
105105
}
106106

107-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
107+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
108108
using (ISession s = OpenSession())
109109
{
110110
ITransaction t = s.BeginTransaction();
@@ -142,7 +142,7 @@ public void UpdateNoWarning()
142142
t.Commit();
143143
}
144144

145-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
145+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
146146
using (ISession s = OpenSession())
147147
{
148148
ITransaction t = s.BeginTransaction();
@@ -179,7 +179,7 @@ public void InsertCascade()
179179
t.Commit();
180180
}
181181

182-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
182+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
183183
using (ISession s = OpenSession())
184184
{
185185
ITransaction t = s.BeginTransaction();
@@ -218,7 +218,7 @@ public void InsertCascadeNoWarning()
218218
t.Commit();
219219
}
220220

221-
using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
221+
using (LogSpy ls = new LogSpy(LogManager.GetLogger(typeof(AssignedFixture).Assembly, "NHibernate"), Level.Warn))
222222
using (ISession s = OpenSession())
223223
{
224224
ITransaction t = s.BeginTransaction();

src/NHibernate.Test/LogSpy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public LogSpy(ILog log) : this(log, false) { }
4040
public LogSpy(System.Type loggerType) : this(LogManager.GetLogger(loggerType), false) { }
4141
public LogSpy(System.Type loggerType, bool disable) : this(LogManager.GetLogger(loggerType), disable) { }
4242

43-
public LogSpy(string loggerName) : this(LogManager.GetLogger(loggerName), false) { }
44-
public LogSpy(string loggerName, bool disable) : this(LogManager.GetLogger(loggerName), disable) { }
43+
public LogSpy(string loggerName) : this(LogManager.GetLogger(typeof(LogSpy).Assembly, loggerName), false) { }
44+
public LogSpy(string loggerName, bool disable) : this(LogManager.GetLogger(typeof(LogSpy).Assembly, loggerName), disable) { }
4545

4646
public MemoryAppender Appender
4747
{

src/NHibernate.Test/NHSpecificTest/Logs/LogsFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public TextLogSpy(string loggerName, string pattern)
6868
Threshold = Level.All,
6969
Writer = new StringWriter(stringBuilder)
7070
};
71-
loggerImpl = (Logger)LogManager.GetLogger(loggerName).Logger;
71+
loggerImpl = (Logger)LogManager.GetLogger(typeof(LogsFixture).Assembly, loggerName).Logger;
7272
loggerImpl.AddAppender(appender);
7373
loggerImpl.Level = Level.All;
7474
}

src/NHibernate.Test/NHSpecificTest/NH1093/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private void NormalList()
8383
protected override void BuildSessionFactory()
8484
{
8585
// Without configured cache, should log warn.
86-
using (var ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn))
86+
using (var ls = new LogSpy(LogManager.GetLogger(typeof(Fixture).Assembly, "NHibernate"), Level.Warn))
8787
{
8888
base.BuildSessionFactory();
8989
Assert.That(ls.GetWholeLog(), Does.Contain("Fake cache used"));

src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using log4net;
12
using log4net.Config;
23
using log4net.Core;
34
using NHibernate.Cfg;
@@ -11,7 +12,7 @@ public class Fixture
1112
[Test]
1213
public void Bug()
1314
{
14-
XmlConfigurator.Configure();
15+
XmlConfigurator.Configure(LogManager.GetRepository(typeof(Fixture).Assembly));
1516
var cfg = new Configuration();
1617
if (TestConfigurationHelper.hibernateConfigFile != null)
1718
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);

src/NHibernate.Test/NHSpecificTest/NH2386/Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
1818

1919
protected override void OnTearDown() {
2020
if (memoryAppender != null) {
21-
var repository = (Hierarchy) LogManager.GetRepository();
21+
var repository = (Hierarchy) LogManager.GetRepository(typeof(Test).Assembly);
2222
repository.Root.RemoveAppender(memoryAppender);
2323
memoryAppender = null;
2424
}

src/NHibernate.Test/NHSpecificTest/NH2779/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Test()
2020

2121
using (var session = OpenSession())
2222
using (var tx = session.BeginTransaction())
23-
using (new LogSpy(LogManager.GetLogger("NHibernate"), Level.All)) // <-- Logging must be set DEBUG to reproduce bug
23+
using (new LogSpy(LogManager.GetLogger(typeof(Fixture).Assembly, "NHibernate"), Level.All)) // <-- Logging must be set DEBUG to reproduce bug
2424
{
2525
Order order = session.Get<Order>("Order-1");
2626
Assert.IsNotNull(order);

src/NHibernate.Test/TestCase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
using NUnit.Framework;
1414
using NHibernate.Hql.Ast.ANTLR;
1515
using System.Collections.Concurrent;
16+
using System.IO;
1617
using NUnit.Framework.Interfaces;
1718
using System.Text;
19+
using log4net.Util;
1820
using static NUnit.Framework.TestContext;
1921

2022
namespace NHibernate.Test
@@ -68,7 +70,7 @@ protected virtual string MappingsAssembly
6870
static TestCase()
6971
{
7072
// Configure log4net here since configuration through an attribute doesn't always work.
71-
XmlConfigurator.Configure();
73+
XmlConfigurator.Configure(LogManager.GetRepository(typeof(TestCase).Assembly));
7274
}
7375

7476
/// <summary>

src/NHibernate.Test/TypesTest/TypeFactoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class TypeFactoryFixture
1414
{
1515
public TypeFactoryFixture()
1616
{
17-
log4net.Config.XmlConfigurator.Configure();
17+
log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(typeof(TypeFactoryFixture).Assembly));
1818
}
1919

2020
private static readonly ILog log = LogManager.GetLogger(typeof(TypeFactoryFixture));

src/NHibernate.Test/UtilityTest/ThreadSafeDictionaryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ThreadSafeDictionaryFixture
1212
{
1313
public ThreadSafeDictionaryFixture()
1414
{
15-
log4net.Config.XmlConfigurator.Configure();
15+
log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(typeof(ThreadSafeDictionaryFixture).Assembly));
1616
}
1717

1818
private static readonly ILog log = LogManager.GetLogger(typeof(ThreadSafeDictionaryFixture));

src/NHibernate/Logging.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Linq.Expressions;
6+
using System.Reflection;
67
using NHibernate.Util;
78

89
namespace NHibernate
@@ -222,16 +223,16 @@ public void WarnFormat(string format, params object[] args)
222223
public class Log4NetLoggerFactory: ILoggerFactory
223224
{
224225
private static readonly System.Type LogManagerType = System.Type.GetType("log4net.LogManager, log4net");
225-
private static readonly Func<string, object> GetLoggerByNameDelegate;
226+
private static readonly Func<Assembly, string, object> GetLoggerByNameDelegate;
226227
private static readonly Func<System.Type, object> GetLoggerByTypeDelegate;
227228
static Log4NetLoggerFactory()
228229
{
229-
GetLoggerByNameDelegate = GetGetLoggerMethodCall<string>();
230+
GetLoggerByNameDelegate = GetGetLoggerByNameMethodCall();
230231
GetLoggerByTypeDelegate = GetGetLoggerMethodCall<System.Type>();
231232
}
232233
public IInternalLogger LoggerFor(string keyName)
233234
{
234-
return new Log4NetLogger(GetLoggerByNameDelegate(keyName));
235+
return new Log4NetLogger(GetLoggerByNameDelegate(typeof(Log4NetLoggerFactory).Assembly, keyName));
235236
}
236237

237238
public IInternalLogger LoggerFor(System.Type type)
@@ -247,6 +248,15 @@ private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
247248
MethodCallExpression methodCall = Expression.Call(null, method, resultValue = keyParam);
248249
return Expression.Lambda<Func<TParameter, object>>(methodCall, resultValue).Compile();
249250
}
251+
252+
private static Func<Assembly, string, object> GetGetLoggerByNameMethodCall()
253+
{
254+
var method = LogManagerType.GetMethod("GetLogger", new[] {typeof(Assembly), typeof(string)});
255+
ParameterExpression nameParam = Expression.Parameter(typeof(string), "name");
256+
ParameterExpression repositoryAssemblyParam = Expression.Parameter(typeof(Assembly), "repositoryAssembly");
257+
MethodCallExpression methodCall = Expression.Call(null, method, repositoryAssemblyParam, nameParam);
258+
return Expression.Lambda<Func<Assembly, string, object>>(methodCall, repositoryAssemblyParam, nameParam).Compile();
259+
}
250260
}
251261

252262
public class Log4NetLogger: IInternalLogger

0 commit comments

Comments
 (0)