Switch to using MS logging abstractions (#267)

This commit is contained in:
Marko Lahma
2025-03-28 19:33:38 +02:00
committed by GitHub
parent 2e6687a60d
commit fda46d95b1
229 changed files with 1084 additions and 898 deletions

View File

@@ -30,9 +30,7 @@
</PropertyGroup>
<ItemGroup>
<Using Include="Common.Logging" />
<Using Include="Common.Logging.ILog" Alias="ILog" />
<Using Include="Common.Logging.LogManager" Alias="LogManager" />
<Using Include="Microsoft.Extensions.Logging.ILogger" Alias="ILog" />
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -44,5 +42,9 @@
<Optimize>true</Optimize>
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Using Include="Microsoft.Extensions.Logging.ILogger" Alias="ILog" />
</ItemGroup>
</Project>

View File

@@ -20,7 +20,11 @@
#region Imports
using System;
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using Spring.Aop.Support;

View File

@@ -19,9 +19,9 @@
#endregion
#region Imports
using System;
using System.Text.RegularExpressions;
using Common.Logging.Simple;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Util;
@@ -44,7 +44,7 @@ namespace Spring.Aop.Support
public void FixtureSetUp()
{
// enable (null appender) logging, to ensure that the logging code is exercised
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
/// <summary>

View File

@@ -18,10 +18,10 @@
#endregion
using Common.Logging.Simple;
using System;
using FakeItEasy;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Objects.Factory;
@@ -44,7 +44,7 @@ namespace Spring.Aop.Target
public void FixtureSetUp()
{
// enable (null appender) logging, just to ensure that the logging code is correct
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
/// <summary>

View File

@@ -20,9 +20,10 @@
#region Imports
using System;
using System.Collections;
using Common.Logging.Simple;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Spring.Aop.Framework;
using Spring.Objects;
@@ -40,27 +41,65 @@ namespace Spring.Aspects.Exceptions
public class ExceptionHandlerAspectIntegrationTests
{
private ExceptionHandlerAdvice exceptionHandlerAdvice;
private CapturingLoggerFactoryAdapter loggerFactoryAdapter;
private ILoggerFactoryAdapter originalAdapter;
private CapturingLoggerFactory loggerFactory;
private ILoggerFactory originalFactory;
private static bool spelActionExecuted = false;
[SetUp]
public void Setup()
{
originalAdapter = LogManager.Adapter;
loggerFactoryAdapter = new CapturingLoggerFactoryAdapter();
LogManager.Adapter = loggerFactoryAdapter;
originalFactory = LogManager.LoggerFactory;
loggerFactory = new CapturingLoggerFactory();
LogManager.LoggerFactory = loggerFactory;
exceptionHandlerAdvice = new ExceptionHandlerAdvice();
}
public class CapturingLoggerFactory : ILoggerFactory
{
public List<string> LoggerEvents { get; set; }= [];
public void Dispose()
{
}
public ILogger CreateLogger(string categoryName)
{
return new CapturingLogger(LoggerEvents);
}
public class CapturingLogger(List<string> loggedEvents) : ILogger
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var message = formatter(state, exception);
loggedEvents.Add(message);
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}
public void AddProvider(ILoggerProvider provider)
{
}
}
[TearDown]
public void TearDown()
{
// loggerFactoryAdapter.LogMessages.Clear();
//reset so other tests can produce some output if needed.
loggerFactoryAdapter.Clear();
LogManager.Adapter = originalAdapter;
loggerFactory.LoggerEvents.Clear();
LogManager.LoggerFactory = originalFactory;
}
[Test]
@@ -111,9 +150,9 @@ namespace Spring.Aspects.Exceptions
catch (ArithmeticException)
{
bool found = false;
foreach (CapturingLoggerEvent loggerEvent in loggerFactoryAdapter.LoggerEvents)
foreach (var loggerEvent in loggerFactory.LoggerEvents)
{
if (loggerEvent.RenderedMessage.IndexOf("Hello World") >= 0)
if (loggerEvent.IndexOf("Hello World") >= 0)
{
found = true;
}
@@ -481,15 +520,15 @@ namespace Spring.Aspects.Exceptions
private void AssertSearchString(string searchString)
{
bool found = false;
foreach (CapturingLoggerEvent loggerEvent in loggerFactoryAdapter.LoggerEvents)
foreach (var loggerEvent in loggerFactory.LoggerEvents)
{
if (loggerEvent.RenderedMessage.IndexOf(searchString) >= 0)
if (loggerEvent.IndexOf(searchString) >= 0)
{
found = true;
}
}
Assert.IsTrue(found, "did not find logging output [" + searchString + "] Logging values = "
+ StringUtils.CollectionToCommaDelimitedString(loggerFactoryAdapter.LoggerEvents));
+ StringUtils.CollectionToCommaDelimitedString(loggerFactory.LoggerEvents));
}
}
}

View File

@@ -18,11 +18,14 @@
#endregion
using System;
using System.Reflection;
using AopAlliance.Intercept;
using FakeItEasy;
using FakeItEasy;
using FakeItEasy.Configuration;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Spring.Aop.Framework;
@@ -60,14 +63,14 @@ namespace Spring.Aspects.Logging
SimpleLoggingAdvice loggingAdvice = new SimpleLoggingAdvice(log);
pf.AddAdvice(loggingAdvice);
A.CallTo(() => log.IsTraceEnabled).Returns(true);
A.CallTo(() => log.IsEnabled(LogLevel.Trace)).Returns(true);
object proxy = pf.GetProxy();
ITestTarget ptt = (ITestTarget)proxy;
ptt.DoSomething();
A.CallTo(() => log.Trace("Entering DoSomething")).MustHaveHappened();
A.CallTo(() => log.Trace("Exiting DoSomething")).MustHaveHappened();
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Entering DoSomething");
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Exiting DoSomething");
}
[Test]
@@ -79,14 +82,14 @@ namespace Spring.Aspects.Logging
MethodInfo mi = typeof(string).GetMethod("ToString", Type.EmptyTypes);
//two additional calls the method are to retrieve the method name on entry/exit...
A.CallTo(() => methodInvocation.Method).Returns(mi);
A.CallTo(() => log.IsTraceEnabled).Returns(true);
A.CallTo(() => log.IsEnabled(LogLevel.Trace)).Returns(true);
A.CallTo(() => methodInvocation.Proceed()).Returns(null);
TestableSimpleLoggingAdvice loggingAdvice = new TestableSimpleLoggingAdvice(true);
loggingAdvice.CallInvokeUnderLog(methodInvocation, log);
A.CallTo(() => log.Trace("Entering ToString")).MustHaveHappened();
A.CallTo(() => log.Trace("Exiting ToString")).MustHaveHappened();
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Entering ToString");
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Exiting ToString");
}
[Test]
@@ -99,8 +102,8 @@ namespace Spring.Aspects.Logging
//two additional calls the method are to retrieve the method name on entry/exit...
A.CallTo(() => methodInvocation.Method).Returns(mi);
A.CallTo(() => log.IsTraceEnabled).Returns(false);
A.CallTo(() => log.IsDebugEnabled).Returns(true);
A.CallTo(() => log.IsEnabled(LogLevel.Trace)).Returns(false);
A.CallTo(() => log.IsEnabled(LogLevel.Debug)).Returns(true);
A.CallTo(() => methodInvocation.Proceed()).Returns(null);
@@ -109,8 +112,8 @@ namespace Spring.Aspects.Logging
Assert.IsTrue(loggingAdvice.CallIsInterceptorEnabled(methodInvocation, log));
loggingAdvice.CallInvokeUnderLog(methodInvocation, log);
A.CallTo(() => log.Debug("Entering ToString")).MustHaveHappened();
A.CallTo(() => log.Debug("Exiting ToString")).MustHaveHappened();
log.VerifyLogMustHaveHappened(LogLevel.Debug, "Entering ToString");
log.VerifyLogMustHaveHappened(LogLevel.Debug, "Exiting ToString");
}
[Test]
@@ -122,7 +125,7 @@ namespace Spring.Aspects.Logging
MethodInfo mi = typeof(string).GetMethod("ToString", Type.EmptyTypes);
//two additional calls the method are to retrieve the method name on entry/exit...
A.CallTo(() => methodInvocation.Method).Returns(mi);
A.CallTo(() => log.IsTraceEnabled).Returns(true);
A.CallTo(() => log.IsEnabled(LogLevel.Trace)).Returns(true);
Exception e = new ArgumentException("bad value");
A.CallTo(() => methodInvocation.Proceed()).Throws(e);
@@ -137,8 +140,8 @@ namespace Spring.Aspects.Logging
{
}
A.CallTo(() => log.Trace("Entering ToString")).MustHaveHappened();
A.CallTo(() => log.Trace("Exception thrown in ToString, ToString", e)).MustHaveHappened();
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Entering ToString");
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Exception thrown in ToString, ToString");
}
[Test]
@@ -154,7 +157,7 @@ namespace Spring.Aspects.Logging
object[] args = new object[] { "hello", luckyNumbers };
A.CallTo(() => methodInvocation.Arguments).Returns(args);
A.CallTo(() => log.IsTraceEnabled).Returns(true);
A.CallTo(() => log.IsEnabled(LogLevel.Trace)).Returns(true);
A.CallTo(() => methodInvocation.Proceed()).Returns(4);
TestableSimpleLoggingAdvice loggingAdvice = new TestableSimpleLoggingAdvice(true);
@@ -164,8 +167,8 @@ namespace Spring.Aspects.Logging
loggingAdvice.CallInvokeUnderLog(methodInvocation, log);
A.CallTo(() => log.Trace(A<string>.That.StartsWith("Entering Bark"))).MustHaveHappened();
A.CallTo(() => log.Trace(A<string>.That.StartsWith("Exiting Bark"))).MustHaveHappened();
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Entering Bark");
log.VerifyLogMustHaveHappened(LogLevel.Trace, "Exiting Bark");
}
}
@@ -176,4 +179,45 @@ namespace Spring.Aspects.Logging
return 4;
}
}
public static class LoggerExtensions
{
public static void VerifyLogMustHaveHappened(this ILogger logger, LogLevel level, string message)
{
try
{
logger.VerifyLog(level, message)
.MustHaveHappened();
}
catch (Exception e)
{
throw new ExpectationException($"while verifying a call to log with message: \"{message}\"", e);
}
}
public static void VerifyLogMustNotHaveHappened(this ILogger logger, LogLevel level, string message)
{
try
{
logger.VerifyLog(level, message)
.MustNotHaveHappened();
}
catch (Exception e)
{
throw new ExpectationException($"while verifying a call to log with message: \"{message}\"", e);
}
}
public static IVoidArgumentValidationConfiguration VerifyLog(this ILogger logger, LogLevel level,
string message)
{
return A.CallTo(() => logger.Log(
level,
A<EventId>._,
A<object>.That.Matches(e => e.ToString().Contains(message)),
A<Exception>._,
A<Func<object, Exception, string>>._)
);
}
}
}

View File

@@ -20,7 +20,8 @@
#region Imports
using Common.Logging.Simple;
using System;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
#endregion
@@ -41,7 +42,7 @@ namespace Spring.Core.IO
public void FixtureSetUp()
{
// enable (null appender) logging, just to ensure that the logging code is correct
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
[Test]
@@ -116,4 +117,4 @@ namespace Spring.Core.IO
}
}
}
}

View File

@@ -20,6 +20,7 @@
#region Imports
using System;
using NUnit.Framework;
#endregion
@@ -124,4 +125,4 @@ namespace Spring.Objects.Factory.Config
"Mmm... the LogFactoryObject class ain't giving back ILog types (it must).");
}
}
}
}

View File

@@ -19,10 +19,9 @@
#endregion
using System.Collections.Specialized;
using Common.Logging.Simple;
using FakeItEasy;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Context.Support;
using Spring.Core.IO;
@@ -49,7 +48,7 @@ namespace Spring.Objects.Factory.Config
public void FixtureSetUp()
{
// enable (null appender) logging, just to ensure that the logging code is correct
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
[Test]
@@ -178,4 +177,4 @@ namespace Spring.Objects.Factory.Config
Assert.AreEqual("Overriden Name", to.Name);
}
}
}
}

View File

@@ -18,9 +18,12 @@
#endregion
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using Common.Logging.Simple;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Collections;
using Spring.Core.IO;
@@ -45,7 +48,7 @@ namespace Spring.Objects.Factory.Xml
public void FixtureSetUp()
{
// enable (null appender) logging, to ensure that the logging code is exercised...
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
//XmlConfigurator.Configure();
}

View File

@@ -14,18 +14,19 @@
* limitations under the License.
*/
using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
#if !NETCOREAPP
using System.Web.Services;
#endif
using Common.Logging.Simple;
using FakeItEasy;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Core.IO;
@@ -56,7 +57,7 @@ namespace Spring.Objects.Factory.Xml
{
// enable (null appender) logging, to ensure that the logging code is exercised...
//XmlConfigurator.Configure();
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
[SetUp]

View File

@@ -20,13 +20,17 @@
#region Imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using Common.Logging.Simple;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Spring.Collections;
using Spring.Core;
@@ -53,8 +57,8 @@ namespace Spring.Objects
[OneTimeSetUp]
public void FixtureSetUp()
{
// enable logging (to nowhere), just to exercisee the logging code...
LogManager.Adapter = new NoOpLoggerFactoryAdapter();
// enable logging (to nowhere), just to exercise the logging code...
LogManager.LoggerFactory = NullLoggerFactory.Instance;
}
#region Classes Used During Tests
@@ -1599,4 +1603,4 @@ namespace Spring.Objects
wrapper.GetPropertyInfo("Bar");
}
}
}
}

View File

@@ -1,3 +1,4 @@
namespace Spring.Messaging.Ems.Core
{
public class SimpleMessageListener : IMessageListener

View File

@@ -1,4 +1,6 @@
using System;
using System.Messaging;
using System.Threading;
namespace Spring.Messaging.Listener
{

View File

@@ -1,3 +1,7 @@
using System;
namespace Spring.Messaging.Listener
{
public class SimpleHandler

View File

@@ -1,3 +1,8 @@
using System;
using System.Threading;
namespace Spring.Messaging.Listener
{
public class WaitingHandler

View File

@@ -20,15 +20,12 @@
using System.Configuration;
using System.Configuration.Internal;
using Common.Logging.Configuration;
using Common.Logging.Simple;
using System.IO;
using NUnit.Framework;
using Spring.Util;
namespace Spring.EnterpriseServices
{
[TestFixture]
public class ExeConfigurationSystemTests
{
@@ -43,9 +40,6 @@ namespace Spring.EnterpriseServices
{
ExeConfigurationSystem ccs = new ExeConfigurationSystem(exePath);
prevConfig = ConfigurationUtils.SetConfigurationSystem(ccs, true);
LogSetting settings = (LogSetting) ConfigurationManager.GetSection("logging");
Assert.AreEqual(typeof (TraceLoggerFactoryAdapter), settings.FactoryAdapterType);
Assert.AreEqual("from custom config!", ConfigurationManager.AppSettings["key"]);
Assert.IsNull(ConfigurationManager.GetSection("spring/context"));

View File

@@ -22,6 +22,7 @@ using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace Spring.Web.Conversation
{
@@ -33,7 +34,7 @@ namespace Spring.Web.Conversation
{
#region Logging
private static readonly Common.Logging.ILog LOG = Common.Logging.LogManager.GetLogger(typeof(SerializeConversationTestModule));
private static readonly ILog LOG = LogManager.GetLogger(typeof(SerializeConversationTestModule));
#endregion
@@ -139,11 +140,11 @@ namespace Spring.Web.Conversation
{
#region Logging
private Common.Logging.ILog LOG
private ILog LOG
{
get
{
return Common.Logging.LogManager.GetLogger(typeof(SerializeConversationTestModule));
return LogManager.GetLogger(typeof(SerializeConversationTestModule));
}
}
@@ -151,7 +152,7 @@ namespace Spring.Web.Conversation
public override Type BindToType(string assemblyName, string typeName)
{
if (LOG.IsDebugEnabled)
if (LOG.IsEnabled(LogLevel.Debug))
LOG.Debug(String.Format("MyBinder.BindToType: {0}, {1}", typeName, assemblyName));
return Type.GetType(typeName + ", " + assemblyName);
}
@@ -201,7 +202,7 @@ namespace Spring.Web.Conversation
{
#region Logging
private static readonly Common.Logging.ILog LOG = Common.Logging.LogManager.GetLogger(typeof(SerializeConversationTestModule));
private static readonly ILog LOG = LogManager.GetLogger(typeof(SerializeConversationTestModule));
#endregion
@@ -209,7 +210,7 @@ namespace Spring.Web.Conversation
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
{
if (LOG.IsDebugEnabled)
if (LOG.IsEnabled(LogLevel.Debug))
LOG.Debug(String.Format("MySerializationSurrogateWrapper.GetObjectData({0},...", obj.GetType()));
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
@@ -231,7 +232,7 @@ namespace Spring.Web.Conversation
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
{
if (LOG.IsDebugEnabled)
if (LOG.IsEnabled(LogLevel.Debug))
LOG.Debug(String.Format("MySerializationSurrogateWrapper.SetObjectData({0},...", obj.GetType()));
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

View File

@@ -1,8 +1,10 @@
using System;
using System.Text;
using Spring;
using Spring.Context;
using Spring.Web.Conversation;
public partial class CircularDependenceTest : System.Web.UI.Page, IApplicationContextAware
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(CircularDependenceTest));

View File

@@ -1,6 +1,7 @@
using Spring.Web.Conversation;
using System.Text.RegularExpressions;
using NHibernate;
using Spring;
using Spring.Data.NHibernate.Support;
using Spring.Objects.Factory;

View File

@@ -1,6 +1,7 @@
using Spring.Web.Conversation;
using Spring.Entities;
using NHibernate;
using Spring;
public partial class SPCLazyLoadTest_A_Status : System.Web.UI.Page
{

View File

@@ -1,6 +1,9 @@
using System;
using Spring.Web.Conversation;
using Spring.Entities;
using NHibernate;
using Spring;
public partial class SPCSwitchConversationSameRequest : System.Web.UI.Page
{

View File

@@ -39,12 +39,6 @@ namespace Spring.Context.Support
[TestFixture]
public class HttpApplicationConfigurerTests
{
[OneTimeSetUp]
public void SetUpFixture()
{
LogManager.Adapter = new Common.Logging.Simple.TraceLoggerFactoryAdapter();
}
[SetUp]
public void SetUp()
{

View File

@@ -1,3 +1,5 @@
using System;
namespace Spring.Data.Objects.Factory.Support
{
public class TestForm : Spring.Web.UI.Page

View File

@@ -40,11 +40,6 @@ namespace Spring.Web.Support
private const string RES_OBJECTS =
"assembly://Spring.Web.Tests/Spring.Web.Support/ControlInterceptionTests.objects.xml";
static ControlInterceptionTests()
{
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.TraceLoggerFactoryAdapter();
}
[SetUp]
public void SetUp()
{