Skip to content
Permalink
Browse files

DI + Logging Refactorings

  • Loading branch information
Trojaner committed Apr 20, 2019
1 parent 99735bb commit 26631c601dd6eb9536d5655ec21db0efba0307cb
@@ -26,44 +26,44 @@ public interface IDependencyContainer : IDependencyResolver, IDisposable
/// <typeparam name="TInterface">The service interface.</typeparam>
/// <typeparam name="TClass">The service implementation.</typeparam>
/// <param name="mappingNames">The mapping names. Include <b>null</b> in mapping names to override default provider.</param>
void RegisterType<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface;
void AddTransient<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface;

/// <summary>
/// Registers a singleton service implementation. Singleton implementation instances are shared between all components.
/// </summary>
/// <typeparam name="TInterface">The service interface.</typeparam>
/// <typeparam name="TClass">The service implementation.</typeparam>
/// <param name="mappingNames">The mapping names. Include <b>null</b> in mapping names to override default provider.</param>
void RegisterSingletonType<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface;
void AddSingleton<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface;

/// <summary>
/// Registers a service implementation. These implementation are not be shared between components.
/// </summary>
/// <typeparam name="TInterface">The service interface.</typeparam>
/// <param name="value">The service implementation instance.</param>
/// <param name="mappingNames">The mapping names. Include <b>null</b> in mapping names to override default provider.</param>
void RegisterInstance<TInterface>(TInterface value, params string[] mappingNames);
void AddTransient<TInterface>(TInterface value, params string[] mappingNames);

/// <summary>
/// Registers a singleton service implementation. Singleton implementation instances are shared between all components.
/// </summary>
/// <typeparam name="TInterface">The service interface.</typeparam>
/// <param name="value">The service implementation instance.</param>
/// <param name="mappingNames">The mapping names. Include <b>null</b> in mapping names to override default provider.</param>
void RegisterSingletonInstance<TInterface>(TInterface value, params string[] mappingNames);
void AddSingleton<TInterface>(TInterface value, params string[] mappingNames);

/// <summary>
/// Unregisters a type.
/// </summary>
/// <typeparam name="T">The type to unregister.</typeparam>
/// <param name="mappingNames">The mapping names to unregister. If null or empty it will unregister everything.</param>
void UnregisterType<T>(params string[] mappingNames);
void Remove<T>(params string[] mappingNames);

/// <summary>
/// Unregisters a type.
/// </summary>
/// <param name="type">The type to unregister.</param>
/// <param name="mappingNames">The mapping names to unregister. If null or empty it will unregister everything.</param>
void UnregisterType(Type type, params string[] mappingNames);
void Remove(Type type, params string[] mappingNames);
}
}
@@ -1,15 +1,16 @@
namespace Rocket.API.DependencyInjection
using Microsoft.Extensions.DependencyInjection;

namespace Rocket.API.DependencyInjection
{
/// <summary>
/// Registers services. The dependency registrator is automatically constructed and called before any plugin loads.
/// </summary>
public interface IDependencyRegistrator
public interface IServiceConfigurator
{
/// <summary>
/// Registers services.
/// </summary>
/// <param name="container">The dependency container.</param>
/// <param name="resolver">The dependency resolver.</param>
void Register(IDependencyContainer container, IDependencyResolver resolver);
void ConfigureServices(IDependencyContainer container);
}
}
@@ -15,8 +15,7 @@ public interface ILogger : IProxyableService
/// <param name="message">The message to log.</param>
/// <param name="exception">The related exception if one exists.</param>
/// <param name="arguments">The message arguments. See <see cref="string.Format(string, object[])" /></param>
void Log(string message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] arguments);
void Log(LogLevel level, object message, Exception exception = null, params object[] arguments);

/// <summary>
/// Checks if a log level is enabled.
@@ -17,7 +17,10 @@
</PackageReference>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'net461'">
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\props\SharedNuGet.props" />

This file was deleted.

@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using Rocket.API;
using Rocket.API.DependencyInjection;
using Rocket.API.User;
using Rocket.Core.User;

namespace Rocket.Console.Properties
{
public class ServiceConfigurator : IServiceConfigurator
{
public void ConfigureServices(IDependencyContainer container)
{
container.AddSingleton<IHost, ConsoleHost>();
container.AddSingleton<IUserManager, StdConsoleUserManager>("host", "stdconsole");
}
}
}
@@ -45,10 +45,10 @@ private void RegisterAssembly(Assembly assembly)
}

logger?.LogTrace("Registering assembly: " + assembly.FullName);
foreach (Type type in assembly.GetTypesWithInterface<IDependencyRegistrator>())
foreach (Type type in assembly.GetTypesWithInterface<IServiceConfigurator>())
{
logger?.LogTrace("\tRegistering from IDependencyRegistrator: " + type.FullName);
((IDependencyRegistrator)Activator.CreateInstance(type)).Register(container, resolver);
logger?.LogTrace("\tRegistering from IServiceConfigurator: " + type.FullName);
((IServiceConfigurator)Activator.CreateInstance(type)).ConfigureServices(container);
}

activatedAssemblies.Add(assembly.FullName);
@@ -45,7 +45,7 @@ private ILogger Logger
public IDependencyContainer CreateChildContainer() => new UnityDependencyContainer(this);
public IDependencyContainer ParentContainer { get; }

public void RegisterSingletonType<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface
public void AddSingleton<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface
{
if (!typeof(ILogger).IsAssignableFrom(typeof(TInterface)))
Logger?.LogTrace("\t\tRegistering singleton: <"
@@ -66,10 +66,10 @@ private ILogger Logger

TInterface instance = container.Resolve<TInterface>(primaryName);
foreach (string name in pendingNames)
RegisterInstance(instance, name);
AddTransient(instance, name);
}

public void RegisterSingletonInstance<TInterface>(TInterface value, params string[] mappingNames)
public void AddSingleton<TInterface>(TInterface value, params string[] mappingNames)
{
if (!typeof(ILogger).IsAssignableFrom(typeof(TInterface)))
Logger?.LogTrace("\t\tRegistering singleton instance: <"
@@ -87,7 +87,7 @@ public void RegisterSingletonInstance<TInterface>(TInterface value, params strin
container.RegisterInstance(mappingName, value, new ContainerControlledLifetimeManager());
}

public void RegisterType<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface
public void AddTransient<TInterface, TClass>(params string[] mappingNames) where TClass : TInterface
{
if (!typeof(ILogger).IsAssignableFrom(typeof(TInterface)))
Logger?.LogTrace("\t\tRegistering type: <"
@@ -105,7 +105,7 @@ public void RegisterSingletonInstance<TInterface>(TInterface value, params strin
container.RegisterType<TInterface, TClass>(mappingName);
}

public void RegisterInstance<TInterface>(TInterface value, params string[] mappingNames)
public void AddTransient<TInterface>(TInterface value, params string[] mappingNames)
{
if (!typeof(ILogger).IsAssignableFrom(typeof(TInterface)))
Logger?.LogTrace("\t\tRegistering type instance: <"
@@ -123,12 +123,12 @@ public void RegisterInstance<TInterface>(TInterface value, params string[] mappi
container.RegisterInstance(mappingName, value);
}

public void UnregisterType<T>(params string[] mappingNames)
public void Remove<T>(params string[] mappingNames)
{
UnregisterType(typeof(T), mappingNames);
Remove(typeof(T), mappingNames);
}

public void UnregisterType(Type type, params string[] mappingNames)
public void Remove(Type type, params string[] mappingNames)
{
foreach (IContainerRegistration registration in container.Registrations
.Where(p => p.RegisteredType == type
@@ -53,8 +53,7 @@ protected BaseLogger(IDependencyContainer container)

public IDependencyContainer Container { get; }

public void Log(string message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] arguments)
public void Log(LogLevel level, object message, Exception exception = null, params object[] arguments)
{
if (!IsEnabled(level))
return;
@@ -90,8 +89,7 @@ public static void SkipMethodFromLogging(MethodBase method)
ignoredLoggingMethods.Add(method);
}

public abstract void OnLog(string message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] bindings);
public abstract void OnLog(object message, LogLevel level = LogLevel.Information, Exception exception = null, params object[] bindings);

public static string GetLogLevelPrefix(LogLevel level)
{
@@ -76,7 +76,7 @@ public void Backup(string file)
}
}

public override void OnLog(string message, LogLevel level = LogLevel.Information, Exception exception = null,
public override void OnLog(object message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] bindings)
{
if (string.IsNullOrEmpty(logFile))
@@ -104,7 +104,7 @@ public void Backup(string file)

if (message != null)
{
WriteLine(level, message, bindings);
WriteLine(level, message.ToString(), bindings);
}

if (exception != null)
@@ -15,7 +15,7 @@ protected FormattedLogger(IDependencyContainer container) : base(container)
SkipTypeFromLogging(GetType());
}

public override void OnLog(string message, LogLevel level = LogLevel.Information, Exception exception = null,
public override void OnLog(object message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] bindings)
{
if (message != null)
@@ -25,7 +25,7 @@ protected FormattedLogger(IDependencyContainer container) : base(container)
WriteLine(level, exception.ToString(), Color.Red);
}

public void WriteLine(LogLevel level, string message, Color? color = null, params object[] bindings)
public void WriteLine(LogLevel level, object message, Color? color = null, params object[] bindings)
{
lock (OutputLock)
{
@@ -40,7 +40,7 @@ public void WriteLine(LogLevel level, string message, Color? color = null, param
WriteColored("] ", Color.White);
}

WriteLineColored(message, color, bindings);
WriteLineColored(message.ToString(), color, bindings);
Console.ResetColor();
}
}
@@ -10,10 +10,12 @@ public class LoggerProxy : ServiceProxy<ILogger>, ILogger
{
public LoggerProxy(IDependencyContainer container) : base(container) { }

public void Log(string message, LogLevel level = LogLevel.Information, Exception exception = null,
params object[] arguments)
public void Log(LogLevel level, object message, Exception exception = null, params object[] arguments)
{
foreach (ILogger service in ProxiedServices) service.Log(message, level, exception, arguments);
foreach (ILogger service in ProxiedServices)
{
service.Log(level, message, exception, arguments);
}
}

public bool IsEnabled(LogLevel level)
@@ -12,7 +12,7 @@ public static void LogTrace(this ILogger logger, string message, params object[]

public static void LogTrace(this ILogger logger, string message, Exception exception, params object[] arguments)
{
logger.Log(message, LogLevel.Trace, exception, arguments);
logger.Log(LogLevel.Trace, message, exception, arguments);
}

public static void LogDebug(this ILogger logger, string message, params object[] arguments)
@@ -22,7 +22,7 @@ public static void LogDebug(this ILogger logger, string message, params object[]

public static void LogDebug(this ILogger logger, string message, Exception exception, params object[] arguments)
{
logger.Log(message, LogLevel.Debug, exception, arguments);
logger.Log(LogLevel.Debug, message, exception, arguments);
}

public static void LogNative(this ILogger logger, string message, params object[] arguments)
@@ -33,7 +33,7 @@ public static void LogNative(this ILogger logger, string message, params object[
public static void LogNative(this ILogger logger, string message, Exception exception,
params object[] arguments)
{
logger.Log(message, LogLevel.Game, exception, arguments);
logger.Log(LogLevel.Game, message, exception, arguments);
}

public static void LogInformation(this ILogger logger, string message, params object[] arguments)
@@ -44,7 +44,7 @@ public static void LogInformation(this ILogger logger, string message, params ob
public static void LogInformation(this ILogger logger, string message, Exception exception,
params object[] arguments)
{
logger.Log(message, LogLevel.Information, exception, arguments);
logger.Log(LogLevel.Information, message, exception, arguments);
}

public static void LogWarning(this ILogger logger, string message, params object[] arguments)
@@ -55,7 +55,7 @@ public static void LogWarning(this ILogger logger, string message, params object
public static void LogWarning(this ILogger logger, string message, Exception exception,
params object[] arguments)
{
logger.Log(message, LogLevel.Warning, exception, arguments);
logger.Log(LogLevel.Warning, message, exception, arguments);
}

public static void LogError(this ILogger logger, string message, params object[] arguments)
@@ -65,7 +65,7 @@ public static void LogError(this ILogger logger, string message, params object[]

public static void LogError(this ILogger logger, string message, Exception exception, params object[] arguments)
{
logger.Log(message, LogLevel.Error, exception, arguments);
logger.Log(LogLevel.Error, message, exception, arguments);
}

public static void LogFatal(this ILogger logger, string message, params object[] arguments)
@@ -75,7 +75,7 @@ public static void LogFatal(this ILogger logger, string message, params object[]

public static void LogFatal(this ILogger logger, string message, Exception exception, params object[] arguments)
{
logger.Log(message, LogLevel.Fatal, exception, arguments);
logger.Log(LogLevel.Fatal, message, exception, arguments);
}
}
}

0 comments on commit 26631c6

Please sign in to comment.
You can’t perform that action at this time.