Skip to content
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

Make IInitLogger inherit ILogger. #7972

Merged
merged 2 commits into from
Mar 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Volo.Abp.Logging;
using Microsoft.Extensions.Logging;
using Volo.Abp.Logging;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionLoggingExtensions
{
public static IInitLogger GetInitLogger(this IServiceCollection services)
public static ILogger<T> GetInitLogger<T>(this IServiceCollection services)
{
return services.GetSingletonInstance<IInitLogger>();
return services.GetSingletonInstance<IInitLoggerFactory>().Create<T>();
}
}
}
}
18 changes: 9 additions & 9 deletions framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public virtual void Dispose()
{
//TODO: Shutdown if not done before?
}

protected virtual void SetServiceProvider(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
Expand All @@ -79,22 +79,22 @@ protected virtual void InitializeModules()
.InitializeModules(new ApplicationInitializationContext(scope.ServiceProvider));
}
}

protected virtual void WriteInitLogs(IServiceProvider serviceProvider)
{
var logger = serviceProvider.GetService<ILogger<AbpApplicationBase>>();
if (logger == null)
{
return;
}
var initLogger = serviceProvider.GetRequiredService<IInitLogger>();

var initLogger = serviceProvider.GetRequiredService<IInitLoggerFactory>().Create<AbpApplicationBase>();

foreach (var entry in initLogger.Entries)
{
logger.LogWithLevel(entry.Level, entry.Message, entry.Exception);
logger.Log(entry.LogLevel, entry.EventId, entry.State, entry.Exception, entry.Formatter);
}

initLogger.Entries.Clear();
}

Expand All @@ -108,7 +108,7 @@ protected virtual IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollec
options.PlugInSources
);
}

//TODO: We can extract a new class for this
protected virtual void ConfigureServices()
{
Expand Down Expand Up @@ -179,4 +179,4 @@ protected virtual void ConfigureServices()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static void AddCoreServices(this IServiceCollection services)
}

internal static void AddCoreAbpServices(this IServiceCollection services,
IAbpApplication abpApplication,
IAbpApplication abpApplication,
AbpApplicationCreationOptions applicationCreationOptions)
{
var moduleLoader = new ModuleLoader();
Expand All @@ -36,7 +36,7 @@ internal static void AddCoreServices(this IServiceCollection services)
services.TryAddSingleton<IModuleLoader>(moduleLoader);
services.TryAddSingleton<IAssemblyFinder>(assemblyFinder);
services.TryAddSingleton<ITypeFinder>(typeFinder);
services.TryAddSingleton<IInitLogger>(new DefaultInitLogger());
services.TryAddSingleton<IInitLoggerFactory>(new DefaultInitLoggerFactory());

services.AddAssemblyOf<IAbpApplication>();

Expand All @@ -49,4 +49,4 @@ internal static void AddCoreServices(this IServiceCollection services)
});
}
}
}
}
30 changes: 12 additions & 18 deletions framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
using System;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

namespace Volo.Abp.Logging
{
public class AbpInitLogEntry
{
public LogLevel Level { get; }

public string Message { get; }

[CanBeNull]
public Exception Exception { get; }

public AbpInitLogEntry(
LogLevel level,
string message,
Exception exception)
{
Level = level;
Message = message;
Exception = exception;
}
public LogLevel LogLevel { get; set; }

public EventId EventId { get; set; }

public object State { get; set; }

public Exception Exception { get; set; }

public Func<object, Exception, string> Formatter { get; set; }

public string Message => Formatter(State, Exception);
}
}
}
30 changes: 22 additions & 8 deletions framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@

namespace Volo.Abp.Logging
{
public class DefaultInitLogger : IInitLogger
public class DefaultInitLogger<T> : IInitLogger<T>
{
public List<AbpInitLogEntry> Entries { get; }

public DefaultInitLogger()
{
Entries = new List<AbpInitLogEntry>();
}

public void Log(
LogLevel logLevel,
string message,
Exception exception = null)

public virtual void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
Entries.Add(new AbpInitLogEntry
{
LogLevel = logLevel,
EventId = eventId,
State = state,
Exception = exception,
Formatter = (s, e) => formatter((TState)s, e),
});
}

public virtual bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None;
}

public virtual IDisposable BeginScope<TState>(TState state)
{
Entries.Add(new AbpInitLogEntry(logLevel, message, exception));
return NullDisposable.Instance;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;

namespace Volo.Abp.Logging
{
public class DefaultInitLoggerFactory : IInitLoggerFactory
{
private readonly Dictionary<Type, object> _cache = new Dictionary<Type, object>();

public virtual IInitLogger<T> Create<T>()
{
return (IInitLogger<T>)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger<T>());;
}
}
}
12 changes: 3 additions & 9 deletions framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Volo.Abp.Logging
{
public interface IInitLogger
public interface IInitLogger<out T> : ILogger<T>
{
public List<AbpInitLogEntry> Entries { get; }

void Log(
LogLevel logLevel,
string message,
Exception exception = null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Volo.Abp.Logging
{
public interface IInitLoggerFactory
{
IInitLogger<T> Create<T>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Volo.Abp.Logging;

namespace Volo.Abp.Modularity
{
internal static class AbpModuleHelper
{
public static List<Type> FindAllModuleTypes(Type startupModuleType, IInitLogger logger)
public static List<Type> FindAllModuleTypes(Type startupModuleType, ILogger logger)
{
var moduleTypes = new List<Type>();
logger.Log(LogLevel.Information, "Loaded ABP modules:");
Expand Down Expand Up @@ -41,7 +40,7 @@ public static List<Type> FindDependedModuleTypes(Type moduleType)
private static void AddModuleAndDependenciesResursively(
List<Type> moduleTypes,
Type moduleType,
IInitLogger logger,
ILogger logger,
int depth = 0)
{
AbpModule.CheckAbpModuleType(moduleType);
Expand Down
13 changes: 6 additions & 7 deletions framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Logging;
using Volo.Abp.Modularity.PlugIns;

namespace Volo.Abp.Modularity
Expand All @@ -26,7 +25,7 @@ public class ModuleLoader : IModuleLoader
}

private List<IAbpModuleDescriptor> GetDescriptors(
IServiceCollection services,
IServiceCollection services,
Type startupModuleType,
PlugInSourceList plugInSources)
{
Expand All @@ -44,16 +43,16 @@ public class ModuleLoader : IModuleLoader
Type startupModuleType,
PlugInSourceList plugInSources)
{
var initLogger = services.GetInitLogger();
var logger = services.GetInitLogger<AbpApplicationBase>();

//All modules starting from the startup module
foreach (var moduleType in AbpModuleHelper.FindAllModuleTypes(startupModuleType, initLogger))
foreach (var moduleType in AbpModuleHelper.FindAllModuleTypes(startupModuleType, logger))
{
modules.Add(CreateModuleDescriptor(services, moduleType));
}

//Plugin modules
foreach (var moduleType in plugInSources.GetAllModules(initLogger))
foreach (var moduleType in plugInSources.GetAllModules(logger))
{
if (modules.Any(m => m.Type == moduleType))
{
Expand Down Expand Up @@ -105,4 +104,4 @@ protected virtual void SetDependencies(List<AbpModuleDescriptor> modules, AbpMod
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Volo.Abp.Logging;

namespace Volo.Abp.Modularity.PlugIns
{
public static class PlugInSourceExtensions
{
[NotNull]
public static Type[] GetModulesWithAllDependencies([NotNull] this IPlugInSource plugInSource, IInitLogger logger)
public static Type[] GetModulesWithAllDependencies([NotNull] this IPlugInSource plugInSource, ILogger logger)
{
Check.NotNull(plugInSource, nameof(plugInSource));

Expand All @@ -19,4 +20,4 @@ public static Type[] GetModulesWithAllDependencies([NotNull] this IPlugInSource
.ToArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Volo.Abp.Logging;
using Microsoft.Extensions.Logging;

namespace Volo.Abp.Modularity.PlugIns
{
public class PlugInSourceList : List<IPlugInSource>
{
[NotNull]
internal Type[] GetAllModules(IInitLogger logger)
internal Type[] GetAllModules(ILogger logger)
{
return this
.SelectMany(pluginSource => pluginSource.GetModulesWithAllDependencies(logger))
.Distinct()
.ToArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Should_Load_Modules_By_Dependency_Order()
var moduleLoader = new ModuleLoader();
var modules = moduleLoader.LoadModules(
new ServiceCollection()
.AddSingleton<IInitLogger>(new DefaultInitLogger()),
.AddSingleton<IInitLoggerFactory>(new DefaultInitLoggerFactory()),
typeof(MyStartupModule),
new PlugInSourceList()
);
Expand All @@ -28,7 +28,7 @@ public class MyStartupModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{

}
}
}
Expand Down