From 88ab13a82da23178c9cbfd5ab6a1cb266f26273e Mon Sep 17 00:00:00 2001 From: Tyler Carrol Date: Sun, 20 Jul 2025 14:25:59 -0400 Subject: [PATCH 1/2] Update comments --- TJC.Singleton/Factories/SingletonFactory.cs | 3 +++ TJC.Singleton/Helpers/SingletonConstructorHelpers.cs | 7 +++++-- TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs | 3 +++ TJC.Singleton/SingletonBase.cs | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/TJC.Singleton/Factories/SingletonFactory.cs b/TJC.Singleton/Factories/SingletonFactory.cs index 7364266..b5ec704 100644 --- a/TJC.Singleton/Factories/SingletonFactory.cs +++ b/TJC.Singleton/Factories/SingletonFactory.cs @@ -4,6 +4,9 @@ namespace TJC.Singleton.Factories; +/// +/// Factory for creating 's. +/// public static class SingletonFactory { #region Constants diff --git a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs index f32fd24..843f3c2 100644 --- a/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonConstructorHelpers.cs @@ -3,12 +3,15 @@ namespace TJC.Singleton.Helpers; +/// +/// Helpers for constructing . +/// public static class SingletonConstructorHelpers { #region Get Singlet Constructor /// - /// Gets the singleton constructor for the type . + /// Gets the singleton constructor for the type . /// /// /// @@ -45,7 +48,7 @@ public static ConstructorInfo GetSingletonConstructor(Type type) #region Check if Singleton has Valid Constructor /// - /// Checks if a singleton of type has a valid constructor. + /// Checks if a singleton of type . has a valid constructor. /// /// /// diff --git a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs index 136e2ba..9961391 100644 --- a/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs +++ b/TJC.Singleton/Helpers/SingletonIdentifierHelpers.cs @@ -1,5 +1,8 @@ namespace TJC.Singleton.Helpers; +/// +/// Helpers for identifying details about . +/// public static class SingletonIdentifierHelpers { /// diff --git a/TJC.Singleton/SingletonBase.cs b/TJC.Singleton/SingletonBase.cs index 810bcae..35016e1 100644 --- a/TJC.Singleton/SingletonBase.cs +++ b/TJC.Singleton/SingletonBase.cs @@ -4,7 +4,7 @@ namespace TJC.Singleton; /// -/// Creates a single instance of that can be accessed through the property. +/// Creates a single instance of that can be accessed through the property. /// /// /// Must have a non-public parameterless constructor. From ba3ebfa812963eedad4e0877d52da1d8c2c7fb4a Mon Sep 17 00:00:00 2001 From: Tyler Carrol Date: Sun, 20 Jul 2025 14:28:28 -0400 Subject: [PATCH 2/2] Replace trace with ILogger --- .../Mocks/Logging/MockLogger.cs | 19 ++++++++++ .../Tests/Instantiated/IsInstantiatedTest.cs | 3 +- TJC.Singleton/Factories/SingletonFactory.cs | 37 +++++++++---------- TJC.Singleton/TJC.Singleton.csproj | 6 +-- 4 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs diff --git a/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs new file mode 100644 index 0000000..004abce --- /dev/null +++ b/TJC.Singleton.Tests/Mocks/Logging/MockLogger.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Logging; + +namespace TJC.Singleton.Tests.Mocks.Logging; + +internal class MockLogger : ILogger +{ + public static MockLogger Default => new(); + + public IDisposable? BeginScope(TState state) + where TState : notnull + => default!; + + public bool IsEnabled(LogLevel logLevel) => true; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + Trace.WriteLine($"[{logLevel}:{eventId}] {formatter(state, exception)}"); + } +} diff --git a/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs b/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs index 4af25d0..7fd86bc 100644 --- a/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs +++ b/TJC.Singleton.Tests/Tests/Instantiated/IsInstantiatedTest.cs @@ -1,4 +1,5 @@ using TJC.Singleton.Factories; +using TJC.Singleton.Tests.Mocks.Logging; namespace TJC.Singleton.Tests.Tests.Instantiated; @@ -13,7 +14,7 @@ public void SingletonGetInstantiatedAfterBeingReferencedTest() MockSingletonInstantiated.IsInstantiated, $"{nameof(MockSingletonInstantiated)} was already instantiated" ); - SingletonFactory.InstantiateAll(trace: true); + SingletonFactory.InstantiateAll(MockLogger.Default); Assert.IsTrue( MockSingletonInstantiated.IsInstantiated, $"{nameof(MockSingletonInstantiated)} is not instantiated" diff --git a/TJC.Singleton/Factories/SingletonFactory.cs b/TJC.Singleton/Factories/SingletonFactory.cs index b5ec704..1920ac2 100644 --- a/TJC.Singleton/Factories/SingletonFactory.cs +++ b/TJC.Singleton/Factories/SingletonFactory.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using Microsoft.Extensions.Logging; using System.Reflection; using TJC.Singleton.Helpers; @@ -20,19 +20,19 @@ private class PlaceholderSingleton : SingletonBase; /// /// Instantiate all singletons in the current app domain. /// - /// /// + /// + /// /// - public static void InstantiateAll(bool trace = true, bool throwIfFailed = false) + public static void InstantiateAll(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace, bool throwIfFailed = false) { var failedToInstantiate = new List(); var singletons = GetSingletonTypes(); - - if (trace) - Trace.WriteLine($"{singletons.Count} Singletons Found"); + + logger?.Log(logLevel, "{count} Singletons Found", singletons.Count); foreach (var singleton in singletons) - if (!singleton.Instantiate(trace)) + if (!singleton.Instantiate(logger, logLevel)) failedToInstantiate.Add(singleton.Name); if (throwIfFailed && failedToInstantiate.Count > 0) @@ -66,34 +66,33 @@ public static List GetSingletonTypes() } /// - /// Instantiates a singleton of type . + /// Instantiates a singleton of type . /// /// - /// + /// + /// /// - public static bool Instantiate(bool trace) => Instantiate(typeof(T), trace); + public static bool Instantiate(ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) => + Instantiate(typeof(T), logger, logLevel); /// /// Instantiates a singleton of given type. /// /// - /// + /// + /// /// /// - private static bool Instantiate(this Type singleton, bool trace) - { - if (trace) - Trace.WriteLine($"[{singleton.Name}] Instantiating"); + private static bool Instantiate(this Type singleton, ILogger? logger = null, LogLevel logLevel = LogLevel.Trace) + { + logger?.Log(logLevel, "[{name}] Instantiating", singleton.Name); var instanceProp = singleton.GetProperty( InstanceName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy ) ?? throw new Exception($"[{singleton.Name}] does not have property [{InstanceName}]"); var instanceValue = instanceProp.GetValue(singleton); - if (trace) - Trace.WriteLine( - $"[{singleton.Name}] {(instanceValue != null ? "Instantiated" : "Failed to Instantiate")}" - ); + logger?.Log(logLevel, "[{name}] {result}", singleton.Name, instanceValue != null ? "Instantiated" : "Failed to Instantiate"); return instanceValue != null; } } diff --git a/TJC.Singleton/TJC.Singleton.csproj b/TJC.Singleton/TJC.Singleton.csproj index 9db1292..6cfc508 100644 --- a/TJC.Singleton/TJC.Singleton.csproj +++ b/TJC.Singleton/TJC.Singleton.csproj @@ -34,15 +34,15 @@ - - - <_Parameter1>TJC.Singleton.Tests + + +